4

Im trying to create an automated test for login page which uses shadow dom. Im using this approach, but so far I was unable to work it out: https://titusfortner.com/2021/11/22/shadow-dom-selenium.html

Here is the login page:

<nbc-input name="username" slot="input" value="" id="username" class="hydrated">
    #shadow-root (open)
    <slot name="element-before"></slot>
    <input type="text">
    <slot name="element-after"></slot>
</nbc-input>

Here is my java code:

WebElement element = driver.findElement(By.id("username"));
SearchContext shadowRoot = element.getShadowRoot();
WebElement shadowElement = shadowRoot.findElement(By.tagName("input"));

Im getting this error:

org.openqa.selenium.InvalidArgumentException: invalid argument: invalid locator

on line:

shadowRoot.findElement(By.tagName("input"));

Im using Selenium 4.3.0, Chrome version 103.0.5060.114, and Chromedriver version 103.0.5060.134.

5
  • @undetectedSelenium And why is that? Commented Aug 4, 2022 at 19:45
  • If you need to know, the whole name is <nbc-input> and I want to access the <input type="text">. Commented Aug 5, 2022 at 6:21
  • Yes I do need help. What do you mean? Im sorry, but I honestly dont understand your comments. Commented Aug 5, 2022 at 7:12
  • This post is not a duplicate. One post is suggesting a 3rd party library and the other is not only linking to the former one, it is also old, using javascript and has nothing to do with selenium 4 as I asked. Commented Aug 5, 2022 at 7:48
  • This particular answer to How to automate shadow DOM elements using selenium? contains what you are missing - only By.id and By.cssSelector appear to work within Shadow DOM. Commented Nov 14, 2022 at 15:57

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.