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.

Grumbunks
  • 497
  • 1
  • 3
  • 10
  • could you add your xpath and html? – Evgeniy Chiruk Jan 24 '20 at 14:07
  • only thing u need to add is `webdriverwait` and you will not get that exception – Pratik Jan 24 '20 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? – Grumbunks Jan 24 '20 at 14:20
  • You don't need the actions line... do you still get the error if you remove that line? – JeffC Jan 24 '20 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 – Grumbunks Jan 24 '20 at 14:32
  • You are sure that the `ElementClickIntercepted` is happening on the `.selectByValue()` call? – JeffC Jan 24 '20 at 14:36
  • @JeffC Yes, absolutely certain. The line where the exception occurs is exactly the line where I call selectByValue. I'm going to test `WebDriverWait` now and I'll return to you with results. – Grumbunks Jan 24 '20 at 14:39
  • Can you post the full error message? Maybe something in there will give us a clue. – JeffC Jan 24 '20 at 14:41
  • @JeffC `ElementClickIntercepted` was the only message. It seems like either increasing the window size or adding `WebDriverWait` as recommended below fixed it. – Grumbunks Jan 24 '20 at 14:59

2 Answers2

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");
Pratik
  • 337
  • 1
  • 9
  • 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. – JeffC Jan 24 '20 at 14:37
  • 1
    This answer is as per the information that we have in the question. – Pratik Jan 24 '20 at 14:42
  • That really doesn't address my comment. If the element wasn't visible, it would be a completely different error message. – JeffC Jan 24 '20 at 14:42
  • 1
    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. – Pratik Jan 24 '20 at 14:48
  • @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. – JeffC Jan 24 '20 at 15:14
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");
    
DebanjanB
  • 118,661
  • 30
  • 168
  • 217
  • 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. – JeffC Jan 24 '20 at 14:37