DEV Community

eRcumenT
eRcumenT

Posted on

Selenium How To Wait Page Loading

I am trying to create an application using Selenium. My functions are exactly as follows. After making selections from some dropdown menus, an animation with the class 'loading' appears on the page, during which all elements are removed and then re-added once the loading is complete. I managed to handle this the way I shared, but I believe there is a more efficient way to do it. Could you please help me?

selectElement: The menu created using ul and li has JavaScript events defined.

optionElement: After making a selection in the selectElement part, the options within the select are loaded, and I check whether they have been loaded.

optionToSelect: Consists of the li elements within the selectElement.

isLoadingExpected: After making some selections, a loading animation appears on the page, which I haven't been able to prevent.

Here's the video and what I want to do(Blurred for privacy): https://streamable.com/p47d93

selectItem(Elements.xxx.xPath, Elements.xxxOptions.xPath, aaa.bbb.xPath, 0);

`public static void selectItem(String selectElement, String optionElements, String optionToSelect, int isLoadingExpected) throws Exception {
WebDriverWait waitElement = new WebDriverWait(chromeDriver, Duration.ofSeconds(10));

    if (isLoadingExpected == 1) {
        waitElement.until(ExpectedConditions.visibilityOfElementLocated(By.className("loading")));
        waitElement.until(ExpectedConditions.invisibilityOfElementLocated(By.className("loading")));
    }

    WebElement selectWebElement = waitElement.until(ExpectedConditions.elementToBeClickable(By.xpath(selectElement)));
    waitElement.until(ExpectedConditions.numberOfElementsToBeMoreThan(By.xpath(optionElements), 1));
    selectWebElement.click();

    WebElement optionWebElement = waitElement.until(ExpectedConditions.elementToBeClickable(By.xpath(optionToSelect)));
    optionWebElement.click();
}`
Enter fullscreen mode Exit fullscreen mode

Top comments (0)