Shadow DOM element automation using Selenium

523 views
Skip to first unread message

Dinu Vijayan

unread,
Mar 4, 2021, 3:10:39 AM3/4/21
to Selenium Users
Hi Everyone,

Our test application was recently upgraded to new UI with Shadow DOM elements. Am unable to find elements in Shadow DOM using normal locators like xpath, css, name, classname etc. When I searched in forums I came across JavaScriptexecutor.Queryselector function using which we can locate elements. It is identifying element but not entering the value in application. The value appears as entered in UI but when we click save the value gets erased. Could you please let me know if there is any methods to handle Shadow DOM in selenium.

Thanks in advance

Andrei Solntsev

unread,
Mar 4, 2021, 3:36:47 AM3/4/21
to seleniu...@googlegroups.com
Just FYI, there is a Selenide library built upon Selenium webdriver. 

Selenide already has methods for operating Shadow DOM elements:
$(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


чт, 4 мар. 2021 г. в 10:10, Dinu Vijayan <vdi...@gmail.com>:
Hi Everyone,

Our test application was recently upgraded to new UI with Shadow DOM elements. Am unable to find elements in Shadow DOM using normal locators like xpath, css, name, classname etc. When I searched in forums I came across JavaScriptexecutor.Queryselector function using which we can locate elements. It is identifying element but not entering the value in application. The value appears as entered in UI but when we click save the value gets erased. Could you please let me know if there is any methods to handle Shadow DOM in selenium.

Thanks in advance

--
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/cd685dfd-3614-44af-b0cf-afc9ecfe5f89n%40googlegroups.com.

Dinu

unread,
Mar 5, 2021, 1:00:43 AM3/5/21
to seleniu...@googlegroups.com
Thanks Andrei, is it possible to integrate Selenide into my existing selenium java framework without migrating my whole project into selenide?



You received this message because you are subscribed to a topic in the Google Groups "Selenium Users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/selenium-users/ZTe35AIRugA/unsubscribe.
To unsubscribe from this group and all its topics, send an email to selenium-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/selenium-users/CAE7WSaGvtfKHtFDvfcvnzW_R7xr11AoL8uNxKbWPLiH2zGmb%2Bw%40mail.gmail.com.

Andrei Solntsev

unread,
Mar 5, 2021, 7:34:41 AM3/5/21
to seleniu...@googlegroups.com

Dinu

unread,
Mar 9, 2021, 12:01:54 AM3/9/21
to seleniu...@googlegroups.com
Thanks Andrei,

I have tried executing using Selenide too but Selenide is also not able to identify the element in shadow DOM and its throwing element not found error.

$(shadowCss("component-id=chrome-add-new-button", "sn-workspace-tabs", "sn-workspace-tab-bar", "now-icon")).should(Condition.visible, Duration.ofSeconds(30));
$(shadowCss("component-id=chrome-add-new-button", "sn-workspace-tabs", "sn-workspace-tab-bar", "now-icon")).click(); 

I have tried the above code.



--
Regards,

Dinu V
Test Lead,
Infosys Ltd | dinu_v...@infosys.com
Mobile: 9633733183 | 07708494402

Andrei Solntsev

unread,
Mar 9, 2021, 1:28:54 AM3/9/21
to seleniu...@googlegroups.com
Hi. I guess you are using the wrong selectors. 
For instance, what is "sn-workspace-tabs"? If this is CSS class name, then you should write ".sn-workspace-tabs". 

Andrei Solntsev


вт, 9 мар. 2021 г. в 07:01, Dinu <vdi...@gmail.com>:

Dinu

unread,
Mar 9, 2021, 2:05:48 AM3/9/21
to seleniu...@googlegroups.com
Hi.. Its the tagName. Please find below screenshot.



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/CAA7CoJgLsca1SH2f2DyqYA7vxt1jeeA0ghtw3YWPZNEvPd7Arg%40mail.gmail.com.

--
You received this message because you are subscribed to a topic in the Google Groups "Selenium Users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/selenium-users/ZTe35AIRugA/unsubscribe.
To unsubscribe from this group and all its topics, send an email to selenium-user...@googlegroups.com.
Screenshot 2021-03-09 at 12.33.55 PM.png

Tony Williams

unread,
Mar 9, 2021, 7:35:19 AM3/9/21
to seleniu...@googlegroups.com
You have to handle only with Javascript. There is no default method available in selenium.

Regards,
Tony Williams

Dinu

unread,
Mar 9, 2021, 7:37:07 AM3/9/21
to seleniu...@googlegroups.com
Hi Andrei..
Sorry, i was wrong. There were issues in the css selector used. I am now able to click on the shadow DOM element. But am not able to enter text in input field. I have tried both setValue and sendKeys methods. Is there any other methods for entering text to a shadow dom input?
--
Regards,

Dinu V

Dinu

unread,
Mar 9, 2021, 7:38:48 AM3/9/21
to seleniu...@googlegroups.com
Hi Tony,
I have used Javascirpt, but the text is not getting entered in the actual input field. In front end it seems like its being entered but it will get cleared if focus moves to the next element.


You received this message because you are subscribed to a topic in the Google Groups "Selenium Users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/selenium-users/ZTe35AIRugA/unsubscribe.
To unsubscribe from this group and all its topics, send an email to selenium-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/selenium-users/CACWHBq_Kho_P7ek%2Bemc%3DThEN76fbeeXXgy9gqUatN5o2i3hgvw%40mail.gmail.com.


--
Regards,

Dinu V

Dinu Vijayan

unread,
Mar 12, 2021, 1:55:17 AM3/12/21
to Selenium Users
Hi Andrei.. 
Please let me know if there are any other methods for entering text to a shadow dom input?

Andrei Solntsev

unread,
Mar 12, 2021, 3:23:28 AM3/12/21
to seleniu...@googlegroups.com
Hi Dinu!
How exactly did you try to enter the text?

пт, 12 мар. 2021 г., 08:55 Dinu Vijayan <vdi...@gmail.com>:

Dinu

unread,
Mar 12, 2021, 3:41:37 AM3/12/21
to seleniu...@googlegroups.com
Hi Andrei,

I have tried the below two ways: But unfortunately both didn't work.

$(shadowCss("input[name=short_description]", "sn-workspace-content", "sn-interaction-custom-renderer",
"now-record-form-connected", "sn-form-internal-workspace-form-layout", "now-record-form-blob", "sn-form-internal-tabs", "sn-form-internal-tab-contents",
"now-record-form-section-column-layout", "sn-record-input-connected")).sendKeys("Test value");
$(shadowCss("input[name=short_description]", "sn-workspace-content", "sn-interaction-custom-renderer",
"now-record-form-connected", "sn-form-internal-workspace-form-layout", "now-record-form-blob", "sn-form-internal-tabs", "sn-form-internal-tab-contents",
"now-record-form-section-column-layout", "sn-record-input-connected")).setValue("Test value");



--
Regards,

Dinu V

Andrei Solntsev

unread,
Mar 12, 2021, 10:48:20 AM3/12/21
to seleniu...@googlegroups.com
Hi Dinu!
Both $.sendKeys() and $.setValue() work for me inside Shadow Dom.

If you found some case where it's not working, please register a github issue at https://github.com/selenide/selenide/issues/new, providing your code and html. 

Andrei Solntsev


пт, 12 мар. 2021 г. в 10:41, Dinu <vdi...@gmail.com>:

Dinu

unread,
Mar 13, 2021, 8:43:38 PM3/13/21
to seleniu...@googlegroups.com
Thanks Andrei,

Am able to enter text in Chrome. Is there anything specific which should be done in Safari browser. I am facing issue in Safari browser. Created a new issue- Please check. 




--
Regards,

Dinu V

Dinu

unread,
Mar 15, 2021, 8:17:19 AM3/15/21
to seleniu...@googlegroups.com
Hi Andrei, 
Do we need to do something extra if we are executing scripts in Safari browser? 

Andrei Solntsev

unread,
Mar 15, 2021, 8:47:52 AM3/15/21
to seleniu...@googlegroups.com
Only a general safari setup (like, you should enable automation somewhere in safari settings). Nothing specific to Selenide.

I saw you registered an issue in Selenide GitHub. Thanks. It needs to be investigated. It seems that shadow Dom and pseudoelements work differently in safari. Probably some functionality doesn't even work at all. 

For example, Firefox driver doesn't allow "sendKeys" inside of shadow Dom, and we can do nothing about it. :(

пн, 15 мар. 2021 г., 14:17 Dinu <vdi...@gmail.com>:

Dinu

unread,
Mar 15, 2021, 11:50:25 PM3/15/21
to seleniu...@googlegroups.com
Thanks Andrei,
I have tried the solution suggested by you in the git issue. But the text entered is getting cleared on subsequent action. Could you please help me with some other solution?




--
Regards,

Dinu V

Andrei Solntsev

unread,
Mar 16, 2021, 2:45:13 AM3/16/21
to seleniu...@googlegroups.com
Do you really think I can help more?
You need to research what is the misterious next action and why it clears the text.

вт, 16 мар. 2021 г., 05:50 Dinu <vdi...@gmail.com>:

Dinu

unread,
Mar 16, 2021, 2:48:03 AM3/16/21
to seleniu...@googlegroups.com
🙂 Thanks Andrei.. was just checking if any other options available.

Reply all
Reply to author
Forward
0 new messages