javaseleniumshadow-root

Selenium | shadow root | Element input is not reachable by keyboard


With Selenium 4.1.2 / Java 11 and a page with "shadow root" elements I have a problem to address a specific input text element.

With this code I reach the input element, the curor is blinking but does not write the sendKeys words:

Thread.sleep(2000);
WebElement inputFIELD = (WebElement) ((JavascriptExecutor)driver).executeScript("return document.querySelector('#TextFieldTEXTFIELD').shadowRoot.querySelector('#vaadin-text-field-input-3 > slot:nth-child(2) > input')");
inputFIELD.sendKeys("test");

The exception to this in the terminal is this:

Exception in thread "main" org.openqa.selenium.ElementNotInteractableException: Element <input> is not reachable by keyboard

I have not found a solution to this so far. Any idea?

I find it strange the cursor find its element but does not write.


Solution

  • You can use JavascriptExecutor to set the values as well.

    //inputFIELD.sendKeys("test");
    ((JavascriptExecutor)driver).executeScript("arguments[0].setAttribute('value', 'test')", inputButton);
    

    or using actions chain:

    new Actions(driver).moveToElement(inputButton).sendKeys("test").build().perform();