Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best practice to wait for a change with 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.

like image 465
Kir Avatar asked May 03 '12 06:05

Kir


1 Answers

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)

like image 185
jersey-city-ninja Avatar answered Sep 26 '22 03:09

jersey-city-ninja