0

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.

9
  • could you add your xpath and html? Commented Jan 24, 2020 at 14:07
  • only thing u need to add is webdriverwait and you will not get that exception Commented Jan 24, 2020 at 14:17
  • @Pratik I already have an implicit wait in my setup with a timeout of 5 seconds, is that not enough or does webdriverwait do something else than an explicit wait? Commented Jan 24, 2020 at 14:20
  • You don't need the actions line... do you still get the error if you remove that line? Commented Jan 24, 2020 at 14:30
  • @JeffC same result as if I didn't have it. My guess is because there's an overlay menu bar at the bottom of the screen that covers it. Currently trying to increase the window size Commented Jan 24, 2020 at 14:32

2 Answers 2

3

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");
Sign up to request clarification or add additional context in comments.

5 Comments

Waiting for element to be visible will not prevent the click intercepted exception. Selenium thinks it's visible, because it is, but the problem is that some HTML is over the element to be clicked and that's why it throws.
This answer is as per the information that we have in the question.
That really doesn't address my comment. If the element wasn't visible, it would be a completely different error message.
I had this error before as well. After adding proper wait for visibility of element, it was solved. Probably, you didn't got this error before, so you wouldn't know about it.
@DebanjanB Yeah... I'm a novice. I've been writing automation for over 20 years with over 5 years of experience with Selenium. I do this for a living and have for the last 25 years. Throwing random stuff at OP and hoping that it fixes it is not the way to approach a problem. If adding a wait fixed this, it was coincidental. If you understood what the exception meant and why it was thrown, we wouldn't be having this discussion.
3

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");
    

1 Comment

Waiting for element to be clickable will not prevent the click intercepted exception. Selenium thinks it's clickable, because it is, but the problem is that some HTML is over the element to be clicked and that's why it throws.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.