Grumbunks
Grumbunks

Reputation: 1277

Can't select option in Select with Selenium

I am having an issue when I try to select an option in a <select> in Selenium.

Select select = new Select(element);
actions.moveToElement(element);
select.selectByValue("100000");

This simply gives me ElementClickIntercepted. Trying to click on it also gives me ElementClickIntercepted. Trying to click on it with JS gives me a NullPointerException. I can very easily select it in Firefox with the element selector so nothing is on top of the select that prevents me from clicking it.

What is intercepting the click? usually when it's because an element is overlaying another, it will tell me in the test results, but here it doesn't.

<div class="pull-left">
<select name="nb" class="form-control">
<option value="10">10</option><option value="20">20</option><option value="50">50</option><option value="100000">All</option>
</select>
</div>

Select xPath:

//select[@name="nb"]

And it is the only select on the page.

Upvotes: 0

Views: 940

Answers (2)

undetected Selenium
undetected Selenium

Reputation: 193338

As the element is a <select> element ideally you need to use Select class. To invoke click() on the option with value as 1000 you need to induce WebDriverWait for the elementToBeClickable() and you can use either of the following Locator Strategies:

  • cssSelector:

    new Select(new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("select.form-control[name='nb']")))).selectByValue("100000");
    
  • xpath:

    new Select(new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//select[@class='form-control' and @name='nb']")))).selectByValue("100000");
    

Upvotes: 3

Pratik
Pratik

Reputation: 357

Try this:

WebDriverWait wait = new WebDriverWait(driver, 40);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(".//select[@name='nb']")));
Select select = new Select(element);
actions.moveToElement(element);
select.selectByValue("100000");

Upvotes: 3

Related Questions