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.

XuBo
  • 73
  • 1
  • 7
  • @undetectedSelenium And why is that? – XuBo Aug 04 '22 at 19:45
  • If you need to know, the whole name is and I want to access the . – XuBo Aug 05 '22 at 06:21
  • Yes I do need help. What do you mean? Im sorry, but I honestly dont understand your comments. – XuBo Aug 05 '22 at 07: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. – XuBo Aug 05 '22 at 07:48
  • This particular answer to [How to automate shadow DOM elements using selenium?](https://stackoverflow.com/a/71010515/2070127) contains what you are missing - only By.id and By.cssSelector appear to work within Shadow DOM. – Easty77 Nov 14 '22 at 15:57

0 Answers0