Initial Approach
Typically (in my experience) a sleep time is required in addition to the wait-time approach; so try combining the two.
For example, I would try looping until the element you want is found. Pseudo-code follows:
boolean found = false;
while (!found) {
try
{
var dropName = new SelectElement(driver.FindElement(By.Id("drop_Name")));
dropName.SelectByText(stringText);
found = true;
}
catch (NoSuchElementException)
{
var dropName = new SelectElement(driver.FindElement(By.Id("drop_Name")));
dropName.SelectByText(stringText);
//do a short sleep here e.g. 500ms depending on the speed of your site
}
}
From the documentation on the InvalidSelectorException, that exception is also thrown when "the selector which is used to find an element does not return a WebElement." So catching NoSuchElementException should be enough.
Alternate Approach
- From your question
<select id="drop_Name">
exists in the code from the start. - Therefore
driver.findElement
will always find a WebElement by IDdrop_Name
- The difference is that until you select
Category1
,drop_Name
has no<option>
values. - Therefore you could try the following wait function:
(Note: code is in Java; can be easily ported to C#)
private static void waitUntilOptionsLoad() {
while(true) {
Thread.sleep(1000);
List<WebElement> options = driver.findElement(By.id("drop_Name"))
.findElements(By.tagName("option"));
if (options.size() > 0 ) {
System.out.println("More than one option tag found; therefore options have loaded");
break;
}
}