3

What do I need to do if drop down take time to populate 1st element from drop down list?

enter image description here

Select Cdropdown = new Select(driver.findElement(By.xpath("//select[@id='ClientId']")));
        Cdropdown.selectByVisibleText("centralpen-ak-01");

Thread.sleep(3000);

Select Edropdown = new Select(driver.findElement(By.xpath("//select[@id='EnvironmentId']")));
            Edropdown.selectByValue("1121");

For loading the drop down it should take the time I have used- Thread.sleep(3000); but still the element is not found. What should I do in such a case?

  • 1
    Is it works for first dropdown? or getting issue with both? – Bharat Mane May 13 '16 at 12:37
  • Its work for first dropdown for second its fail – Mahesh May 13 '16 at 12:57
  • What error are you getting? – IAmMilinPatel May 14 '16 at 3:06
  • Error which got after ran the code: Cannot locate element with text: centralpen-prod-m-01 For documentation on this error, please visit: seleniumhq.org/exceptions/no_such_element.html – Mahesh May 16 '16 at 9:37
12

Do you think that time is the problem here?

If so try to increase the time you are providing in Thread.sleep(); and see it that works out for you.

If its not the time, then did you try to find out what the problem is? Are you getting any error?

  • While that's one thing to consider trying, we may want to ask ourselves if Thread.sleep() is what we want at all - do we want to rely on a certain amount of time if time itself isn't a requirement? – corsiKa May 13 '16 at 16:58
  • Increase sleep time? And this is most upvoted answer? Will THIS be representative of the future quality of the answers here? Sad day... – Peter M. May 16 '16 at 14:40
3

adding Thread.sleep(3000) is not a good way to handle synchronization, rather use implicit wait.

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

Implicit wait allows your browser to wait until element is visible or until time set by you, whichever is earlier.

3

1:

Select Cdropdown = new Select(driver.findElement(By.xpath("//select[@id='ClientId']")));
Cdropdown.selectByVisibleText("centralpen-ak-01");

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

Select Edropdown = new Select(driver.findElement(By.xpath("//select[@id='EnvironmentId']")));
                    Edropdown.selectByIndex(0);
//check your index, starting from 0 which item you have to select & add corresponding index of that.  

OR
2:
You may try with same method-selectByVisibleText as like 1st dropdown

3: Otherwise You may try with explicitly wait-

 //Selecting value from 2nd drop down using visible text

    Select mydrpdwn = new Select(driver.findElement(By.id("EnvironmentId")));
    mydrpdwn.selectByVisibleText("add element in dropdown");

    WebDriverWait wait = new WebDriverWait(driver, 15);
    wait.until(ExpectedConditions.elementToBeClickable(By.id("EnvironmentId")));
  • I try both way but still the script is not work. I think its application issue for populating the drop down – Mahesh May 16 '16 at 13:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.