Selenium - Shadows DOM

35 views
Skip to first unread message

Fabio Giusti

unread,
Feb 19, 2021, 11:59:09 PM2/19/21
to Selenium Users
Hello everyone, I have been using Selenium for a few months to create test cases in java and now I came across a website with "shadow dom" and the Xpath commands (which I prefer) do not work or I can not get them to work correctly, you can indicate some solution to create tests or instructions?
to know if by chance with the new version Selenium 4 it works with the elements in shadow dom?
Thank you...

Arvind Sah

unread,
Feb 20, 2021, 3:23:53 AM2/20/21
to seleniu...@googlegroups.com
Hi,

Selenium doesn't support locating element under shadow dom. To work with shadow dom element you have to use javascript executor.

1. To start with first have basic understanding about what is shadow dom element and how to check if they are closed shadow dom or open (we can't locate elements under closed shadow dom)

2. Once you have basic understanding about shadow then you are all set you go ahead.

3. Open the websit on chrome browser, right click on the element and copy the js selector which will be used to locate the element over shadow dom. To cross check run the copied content under console.

 4. Once you are done #3, use the same js selector as js executor to execute the query which will return you the webelement.

Hope his helps you to get started.

Happy learning, 
Arvind

--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/selenium-users/1efe6f02-1bed-4ae4-b58a-de41929cc169n%40googlegroups.com.

Andrei Solntsev

unread,
Feb 20, 2021, 3:38:43 AM2/20/21
to seleniu...@googlegroups.com
Hi Fabio. 

Just FYI, in Selenide we do support Shadow DOM. Here is how the syntax looks like:
$(shadowCss("#anyButton", "#shadow-host")).click();

$(shadowCss("p", "#shadow-host")).shouldHave(text("Inside Shadow-DOM"));

$(shadowCss("p", "#shadow-host", "#inner-shadow-host")).shouldHave(text("The Shadow-DOM inside another shadow tree"));

Andrei Solntsev


сб, 20 февр. 2021 г. в 06:59, Fabio Giusti <fabio19...@gmail.com>:
Hello everyone, I have been using Selenium for a few months to create test cases in java and now I came across a website with "shadow dom" and the Xpath commands (which I prefer) do not work or I can not get them to work correctly, you can indicate some solution to create tests or instructions?
to know if by chance with the new version Selenium 4 it works with the elements in shadow dom?
Thank you...

--

Fabio Giusti

unread,
Feb 22, 2021, 4:20:40 AM2/22/21
to Selenium Users
thanks, some help and information

Serguei Kouzmine

unread,
Feb 23, 2021, 9:47:33 PM2/23/21
to Selenium Users

There  are few Java adapter projects
https://github.com/sukgu/shadow-automation-selenium 
offering Selenium api to Shadow DOM DOM like

List<WebElement> elements = shadow.findElements("some css selector");  
shadow.findElementByXPath("some xpath")  
I contributed to the above project and assure you it has the above 

feel free to pist morespecific questions

Serguei Kouzmine
Reply all
Reply to author
Forward
0 new messages