Huma
Huma

Reputation: 71

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

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(); 

Upvotes: 3

Views: 12348

Answers (6)

Usman Kokab
Usman Kokab

Reputation: 193

Using Proper X.path reaching into the option can be easy solution here. Try the following code.

WebDriverWait wait = new WebDriverWait(driver, 30); wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//select[@name='Location']/option[@value='1']")));

This will see if element in option has been loaded, if not it will wait until it is loaded into DOM for given seconds.

Upvotes: 2

Robert Kaszubowski
Robert Kaszubowski

Reputation: 11

I faced this same problem some time ago. This is my solution working with Java 8:

    void selectTextFromDropDown(final By locator, final String value, final int timeoutInSeconds) {
    FluentWait<WebDriver> wait = createWait(timeoutInSeconds);
    wait.until(input -> {
        Select mySelect = new Select(input.findElement(locator));
        List<WebElement> options = mySelect.getOptions();
        for (WebElement option : options) {
            if (option.getText().equalsIgnoreCase(value)) {
                option.click();
                mySelect.getAllSelectedOptions().contains(value.toLowerCase());
                break;
            }
        }
        return true;
    });
}

Upvotes: 1

Ajinkya
Ajinkya

Reputation: 22720

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());
            }
        });

Upvotes: 5

ravi.patel
ravi.patel

Reputation: 119

You should use wait() and notify() methods. The place you have written Thread.sleep() method, replace it with this.wait(). And the place where the load data of your dropdown is completed put there this.notify()method. I hope this will help u.

Upvotes: 0

Shyamala
Shyamala

Reputation: 69

You can use like the following:

//To type text in drop down 
driver.findElement(By.id("ur id")).sendKeys("txt");

//Use implicit wait to load the drop down
driver.manage().timeouts().implicitlyWait(250, TimeUnit.MILLISECONDS)

//Then click on the value in the drop down
new WebDriverWait(driver,30).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//li[@class='ui-menu-item'][5]"))).click()

//Now click the next drop down after clicking the drop down value
driver.findElement(By.className("buttonname")).click()

Upvotes: 0

BevynQ
BevynQ

Reputation: 8269

Something like this should do what you want.

for (int second = 0;; second++) {
    if (second >= 60) fail("timeout");
    try { 
        Select droplist = new Select(driver.findElement(By.Id("selection")));
        if(!droplist.getOptions().isEmpty()){
            break;
        }
    } catch (Exception e) {
         // best put something here
    }
    Thread.sleep(1000);
}

Upvotes: 0

Related Questions