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?

4
  • 1
    Is it works for first dropdown? or getting issue with both? May 13, 2016 at 12:37
  • Its work for first dropdown for second its fail
    – Mahesh
    May 13, 2016 at 12:57
  • What error are you getting? May 14, 2016 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, 2016 at 9:37

4 Answers 4

11

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?

2
  • 1
    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, 2016 at 16:58
  • 1
    Increase sleep time? And this is most upvoted answer? Will THIS be representative of the future quality of the answers here? Sad day... May 16, 2016 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.

1
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")));
1
  • I try both way but still the script is not work. I think its application issue for populating the drop down
    – Mahesh
    May 16, 2016 at 13:31
0

try following code:

WebDriverWait wait;
wait = new WebDriverWait(driver, Duration.ofSeconds(15));
wait.until(ExpectedConditions.visibilityOf(WebElement));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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