Hey so I am trying to click a Button which is located in a Shadow-Root, I am new to this topic and this is my first time working with Shadow Roots.
Some Information: I am working in Visual Studio Code on a Mac, C# using Selenium and the Chromedriver. Opening Chrome and clicking and filling Textbooks out of the Shadow-Root works.
Thanks in Advance for your Help!!
I also tried to use the driver.executescript but I can’t manage to get it working, it says that something with the executescript is missing (beginner problem I guess).
Result should be that the Accept (Akzeptieren) Button will be clicked.
2
Answers
try this, If you are using selenium version > 4 , it has native shadow dom support
To interact with elements inside a Shadow DOM (Shadow Root) using Selenium and C#, you need to execute JavaScript code in the browser’s context to access the desired element. Here’s an example of how you can click the "Accept" (Akzeptieren) button on the website you provided:
In this code, we first locate the shadow host element using its CSS selector. Then, we use JavaScript execution to access the shadow root by retrieving the
shadowRoot
property of the shadow host element. Finally, we find the Accept button within the shadow root using its ID and click it.Make sure you have the Selenium WebDriver and ChromeDriver packages installed in your project. You can install them via NuGet Package Manager or by running the following command in the Package Manager Console:
Remember to adjust the path to the ChromeDriver executable if necessary.
I hope this helps you interact with the Accept button inside the Shadow DOM. Let me know if you have any further questions!