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
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)
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.
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.
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
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.