¿La mejor práctica para esperar un cambio con Selenium Webdriver?

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 focus via a JS)

After spending time searching for a reliable alternative to "waitForAttribute" (selenium 1 command) in web driver... I could get the below code to work. But I am not sure if this is the best implementation...

Any other better solution??

wait = new WebDriverWait(wedriver1, TimeSpan.FromSeconds(5));
.....
button.Click();
wait.Until(webdriver1 => webdriver2.webelement.GetAttribute("style").Contains("display: block"));

Also, can anyone please share a link to how I can handle AJAX event changes using webdriver.

preguntado el 03 de mayo de 12 a las 07:05

assertselenium.com/2013/01/29/webdriver-wait-commands has a nice listing of possible wait methods and how they relate -

@Pat: the assertselenium.com website seems to be gone. -

5 Respuestas

Sugiero usar org.openqa.selenium.support.ui.ExpectedConditions.attributeToBe(WebElement element, String attribute, String value).

p.ej

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

(documentos)

Respondido 19 Jul 17, 09:07

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.

contestado el 03 de mayo de 12 a las 07:05

This will work, but only for situations where an element appears/disappears. There are many situations where an element remains visible but changes an attribute, which I think the OP was asking for, which these methods don't cover. - Vince Bowdren

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.

Más aquí: http://seleniumhq.org/docs/04_webdriver_advanced.html#implicit-waits

contestado el 04 de mayo de 12 a las 18:05

Sorry, I don't think you understood my question. My elements attribute changes on a click event (driven by JS). I don't see how the implicit wait is useful here. From my understanding implicit wait is more relevant in case of elements being visible after an event. - Kir

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.

contestado el 07 de mayo de 12 a las 05:05

You could use Thread.sleep like people have implemented aquí..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.

contestado el 23 de mayo de 17 a las 13:05

I believe this whole while loop is what the "wait.Until" I posted above is doing. So I don't see the point of writing this explicit while loop. I suppose my implementation which is working is a lot cleaner - Kir

while(timeout<10) y eliminar if statement. Just a note to your code. - Aleh Duhi

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.