19

I am using Selenium for the first time and am overwhelmed by the options. I am using the IDE in Firefox.

When my page loads, it subsequently fetches values via an JSONP request, with which it populates options in a select.

How do I get Selenium to wait for a certain option in the select to be present before proceeding?

mydoghasworms
  • 18,233
  • 11
  • 61
  • 95

9 Answers9

13

I used waitForElementPresent with a css target.

Example: To wait for

<select id="myselect"></select>

to be populated with

<option value="123">One-two-three</option>

use

  • Command: waitForElementPresent
  • Target: css=#myselect option[value=123]
  • Value: (leave it empty)
user1460043
  • 2,331
  • 1
  • 19
  • 26
3

I found handy something like:

wait.until(
    ExpectedConditions
        .presenceOfNestedElementsLocatedBy(By.id(<selectioNId>), By.tagName("option"))
)
MarkoCen
  • 2,189
  • 13
  • 24
  • this is perfect for my needing: I know that there is a select element, but I needed to understand if the value is present in the options before to retrieve the elements, avoiding try to find put a value which it wasn't present. – Salvatore Pannozzo Capodiferro Mar 22 '18 at 15:56
2

You can use "WaitForSelectOption" command where your value can be direct label like label=1-saving Account target will have the object id

Manas
  • 71
  • 1
  • 2
  • 7
2

I made the following function in C# that returns the select when is populated.

You have to pass a By to find the element and you a specific time to wait for it to be filled:

public static SelectElement FindSelectElementWhenPopulated(this IWebDriver driver, By by, int delayInSeconds)
    {
        WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(delayInSeconds));
        return wait.Until<SelectElement>(drv => 
        {
            SelectElement element = new SelectElement(drv.FindElement(by));
            if (element.Options.Count >= 2)
            {
                return element;
            }

            return null;
        }
        );
    }

In my case I validate tha the select has more than 2 options, you can change the code so that it validates the quantity that fits your needs.

Felipe Correa
  • 654
  • 9
  • 23
2

We had similar problem where the options within drop down was fetched from a third party service, which sometimes was a slower operation.

Select select = new Select(driver.findElement(cssSelector("cssSelectorOfSelectBox")));    
waitUnitlSelectOptionsPopulated(select)

here is the definition for waitUnitlSelectOptionsPopulated

private void waitUntilSelectOptionsPopulated(final Select select) {
            new FluentWait<WebDriver>(driver)
                    .withTimeout(60, TimeUnit.SECONDS)
                    .pollingEvery(10, TimeUnit.MILLISECONDS)
                    .until(new Predicate<WebDriver>() {
                        public boolean apply(WebDriver d) {
                            return (select.getOptions().size() > 1);
                        }
                    });
        }

Note: A check of select.getOptions().size() >1 was needed in our case as we had one static option always displayed.

Sanjay Bharwani
  • 3,317
  • 34
  • 31
1

I think you should be use

waitForElementPresent command. If possible let's me see your selenium IDE code.

1

Using java 8 Awaitility

Awaitility.await()
.atMost(5, TimeUnit.SECONDS)
.until(() -> select.getOptions().size() > 0);
Regolith
  • 2,944
  • 9
  • 33
  • 50
ibenjelloun
  • 7,425
  • 2
  • 29
  • 53
1

Try the below code

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException

try:
    # 10 is the maximum time to wait
    element = WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.CSS_SELECTOR, "#mySelectId option[value='valueofOptionYouExpectToBeLoaded']"))
    )
except TimeoutException:
    print("Time out")

#  Write your code
Sudheer Muhammed
  • 813
  • 8
  • 10
  • Can you explain why I should try this code? What's better about it than the existing answers, specifically? Thanks. – ggorlen Apr 13 '22 at 17:20
0

Its work for me:

  WebElement element = driver.findElement(By.xpath("//select[@name='NamE']/option[1]"));
  return element.getAttribute("text");
helloWorld
  • 219
  • 2
  • 11