Best practice to wait for a change with Selenium Webdriver?

前端 未结 5 2325
情话喂你
情话喂你 2021-02-13 02:08

After a click event I need to wait for an elements attribute to change before proceeding further (click event causes certain elements to move out of focus and certain others get

相关标签:
5条回答
  • 2021-02-13 02:48

    I suggest use org.openqa.selenium.support.ui.ExpectedConditions.attributeToBe(WebElement element, String attribute, String value).

    e.g.

    WebDriverWait wait = new WebDriverWait(driver, 5); // time out after 5 seconds
    someElement.click();
    wait.until(ExpectedConditions.attributeToBe(someElement, "sort-attribute", "ascending"));
    

    (docs)

    0 讨论(0)
  • 2021-02-13 02:54

    I feel that using the WebDriverWait is pretty useful. One change you can make in your code is use -

    webelement.isDisplayed(); 
    

    instead of getting the style attribute.

    0 讨论(0)
  • 2021-02-13 02:55

    It's a recurring problem of Selenium. I am not sure about a "best" implementation existing. I believe it's largely depending on the situation.

    Concerning AJAX-driven changes, I would generally use a waitForElementPresent or a waitForElementNotPresent depending on the changes the AJAX call will make on the page.

    0 讨论(0)
  • 2021-02-13 02:58

    You can use implicit WebDriver wait :

    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

    An implicit wait is to tell WebDriver to poll the DOM for a certain amount of time when trying to find an element or elements if they are not immediately available.

    More here: http://seleniumhq.org/docs/04_webdriver_advanced.html#implicit-waits

    0 讨论(0)
  • 2021-02-13 02:59

    You could use Thread.sleep like people have implemented here..You can use their implementations to relate to your problem.

        public void waitFor(Webdriver driver,,WebElement welElem, String attributeValue){
        int timeout=0;
        while(true){
            if(driver.webElem.getAttribute(attribute)!=null){
            break;
            }
            if(timeout>=10){
            break;
            }
            Thread.sleep(100);
            timeout++;
        }
    
    }
    

    I think you could implement something like this. I have not checked it but you get the idea.

    0 讨论(0)
提交回复
热议问题