Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in DevOps and Agile by (19.7k points)

I am writing tests for a web application. Some commands pull up dialog boxes that have controls that are visible, but not available for a few moments. (They are greyed out, but webdriver still sees them as visible).

How can I tell Selenium to wait for the element to be actually accessible, and not just visible?

    try:

        print "about to look for element"

        element = WebDriverWait(driver, 10).until(lambda driver : driver.find_element_by_id("createFolderCreateBtn"))

        print "still looking?"

    finally: print 'yowp'

Here is the code that I have tried, but it "sees" the button before it is usable and basically charges right past the supposed "wait".

Note that I can stuff a ten second sleep into the code instead of this and the code will work properly, but that is ugly, unreliable, and inefficient. But it does prove that the problem is just that "click" command is racing ahead of the availability of the controls.

1 Answer

0 votes
by (62.9k points)

If you want the WebDriver to wait until the element is visible on the web application then Selenium WebDriverdriver/Selenium 2 has its own method named visibilityOfElementLocated(By locator) to check the visibility of element on software web applications. You can use this method with an explicit webdriver wait condition to wait until the element of a web-application is visible. 

Syntax to wait until the element is visible on the web-application is shown below. It will wait for a max of 15 seconds for the element. As soon as the element is visible on the page, the webdriver will go on further to execute the next statement.

WebDriverWait wait = new WebDriverWait(driver, 15);

wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//input[@id='text3']")));

@Test Method Part

@Test

 public void test () throws InterruptedException, IOException 

 {   

  //To wait for element visible

  WebDriverWait wait = new WebDriverWait(driver, 15);

  wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//input[@id='text3']")));

  driver.findElement(By.xpath("//input[@id='text3']")).sendKeys("Text box is visible now");

  System.out.print("Text box text3 is now visible");

}

...