DEV Community

eRcumenT
eRcumenT

Posted on

1

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

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Immerse yourself in a wealth of knowledge with this piece, supported by the inclusive DEV Community—every developer, no matter where they are in their journey, is invited to contribute to our collective wisdom.

A simple “thank you” goes a long way—express your gratitude below in the comments!

Gathering insights enriches our journey on DEV and fortifies our community ties. Did you find this article valuable? Taking a moment to thank the author can have a significant impact.

Okay