Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write wait in Selenium web Driver until values get filled in drop down and then click on a button

Tags:

java

selenium

In my application when i opens a page a dropdown displays and then i need to click on a proceed button. Problem is drop down takes some time to load the values but in my code it click before the drop down loads.I tried with implicit wait and thread.sleep but it some time it work and some time doesn't work. Code:

       public class Home {

    public static void main(String[] args) throws IOException, InterruptedException 
    {
    File file1 = new File("C:\\Selenium\\IEDriverServer_Win32_2.35.3\\IEDriverServer.exe");
      System.setProperty("webdriver.ie.driver", file1.getAbsolutePath());

   WebDriver driver = new InternetExplorerDriver();
    driver.get("http://10.120.13.100/");

     driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);

    Thread.sleep(3000);

    WebElement clickBtn = driver.findElement(By.id("btnHomeProceed"));
   clickBtn.click(); 
like image 267
Huma Avatar asked Dec 18 '25 13:12

Huma


1 Answers

You can use FluentWait

final Select droplist = new Select(driver.findElement(By.Id("selection")));
new FluentWait<WebDriver>(driver)
        .withTimeout(60, TimeUnit.SECONDS)
        .pollingEvery(10, TimeUnit.MILLISECONDS)
        .until(new Predicate<WebDriver>() {

            public boolean apply(WebDriver d) {
                return (!droplist.getOptions().isEmpty());
            }
        });
like image 65
Ajinkya Avatar answered Dec 21 '25 00:12

Ajinkya