16

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.

Mehul Kabaria
  • 6,404
  • 4
  • 25
  • 50
Kir
  • 385
  • 1
  • 7
  • 20
  • http://assertselenium.com/2013/01/29/webdriver-wait-commands/ has a nice listing of possible wait methods and how they relate – Pat May 06 '14 at 22:33
  • @Pat: the assertselenium.com website seems to be gone. – Vince Bowdren Apr 27 '15 at 09:29
  • 1
    Archived versions of that page: http://web.archive.org/web/*/http://assertselenium.com/2013/01/29/webdriver-wait-commands/ – Pat Apr 27 '15 at 17:25

5 Answers5

9

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)

Steve Cooper
  • 20,542
  • 15
  • 71
  • 88
jersey-city-ninja
  • 1,038
  • 11
  • 23
6

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.

Silver Quettier
  • 2,045
  • 2
  • 26
  • 53
  • 3
    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 Apr 27 '15 at 09:32
4

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

Manpreet Singh
  • 3,643
  • 2
  • 18
  • 14
  • 1
    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 May 05 '12 at 05:58
2

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.

Hari Reddy
  • 3,808
  • 4
  • 33
  • 42
0

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.

Community
  • 1
  • 1
John
  • 619
  • 2
  • 9
  • 24
  • 2
    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 May 05 '12 at 05:55
  • 1
    `while(timeout<10)` and delete `if` statement. Just a note to your code. – Aleh Douhi May 05 '12 at 20:26