Home  >  Article  >  Java  >  Selenium How To Wait Page Loading

Selenium How To Wait Page Loading

Linda Hamilton
Linda HamiltonOriginal
2024-09-30 06:10:031003browse

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();
}`

The above is the detailed content of Selenium How To Wait Page Loading. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn