0

Consider the following <select> from an internal Selenium test page:

<select id="invisi_select" style="opacity:0;">
    <option selected value="apples">Apples</option>
    <option value="oranges">Oranges</option>
</select>

It is used to simulate an invisible element as the id suggests, which is done by setting opacity to 0.

Although the element is not visible, a user can actually interact with it. If I open the page in a browser and click on the element's position, the select menu opens. I believe this is also why WebElement#isDisplayed() returns true for this element, which is also what these old Selenium issues suggest:

To execute actions such as clicks, we recently switched to the Java interactions API for several reasons, e.g., to prevent ElementClickInterceptedExceptions. (Please note that this is not about refactoring a bunch of Selenium tests, this happens in the context of a generic action executor that operates on top of the Selenium API.) However, if I do something like:

WebElement applesOption = /* get apples option */
new Actions(webDriver).moveToElement(applesOption)
        .click()
        .perform();

Moving to the element throws the following exception:

org.openqa.selenium.JavascriptException: javascript error: Failed to execute 'elementsFromPoint' on 'Document': The provided double value is non-finite.

I guess this is because elementsFromPoint() via the WebDriver Actions API seems to return a "non-finite" double for transparent elements like this?

Is there a way to prevent this from happening when using Actions? Maybe, in addition to checking if the element is clickable (ExpectedConditions#elementToBeClickable(...)), I would have to parse—which sounds horrible—attributes such as opacity?

beatngu13
  • 7,201
  • 6
  • 37
  • 66
  • Rather than refactoring clicks to use Actions, did you try my answer [here](https://stackoverflow.com/a/58698043/2386774)? We use this in all our projects and it works with no issues. – JeffC Nov 05 '19 at 18:29
  • @JeffC Unfortunately, this is only applicable to a certain degree in my context as it doesn't _prevent_ the exception from happening in the first place. – beatngu13 Sep 01 '20 at 11:33

2 Answers2

1

I just tried your sample file locally and the code below is working with no exceptions.

WebElement e = driver.findElement(By.id("invisi_select"));
Select select = new Select(e);
select.selectByValue("apples");
System.out.println(select.getFirstSelectedOption().getText());
select.selectByValue("oranges");
System.out.println(select.getFirstSelectedOption().getText());

It prints

Apples
Oranges
JeffC
  • 22,180
  • 5
  • 32
  • 55
  • Please consider my note: "[…] this happens in the context of a generic action executor that operates on top of the Selenium API." I also said that I'm using the interactions API / `Actions`, where `Select` is not applicable. Nonetheless, +1 for presenting a solution I wasn't aware of. Thx! – beatngu13 Sep 01 '20 at 20:46
  • Yes but you said you switched over to `Actions` when the regular clicks, etc. were throwing `ElementClickInterceptedException`. This code doesn't throw any exceptions so you can use it instead of `Actions`. – JeffC Sep 02 '20 at 00:52
1

This error message...

org.openqa.selenium.JavascriptException: javascript error: Failed to execute 'elementsFromPoint' on 'Document': The provided double value is non-finite.

...implies that the WebDriver instance was unable to focus on the element for one or other reasons:

  • The element havn't loaded properly when you tried to interact with it.
  • The element haven't got the focus.

Solution

Instead of using the Java interactions API you can use the Select Class and you can use either of the following Locator Strategies:

  • Using cssSelector and selectByValue():

    Select s = new Select(driver.findElement(By.cssSelector("select#invisi_select")));
    s.selectByValue("apples");
    
  • Using xpath and selectByVisibleText():

    Select s = new Select(driver.findElement(By.xpath("//select[@id='invisi_select']")));
    s.selectByVisibleText("Apples");
    

References

You can find a couple of relevant detailed discussions in:

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352