Search code examples
c#selenium-webdriverselenium-chromedrivershadow-domshadow-root

How to click a Button which is located under a Shadow-Root


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.

The Website: https://www.all4golf.de/?gclid=CjwKCAjw67ajBhAVEiwA2g_jEP_x3w6PJibWICIaE61LtLDJO_EEy85mgKRseNvZIAJpsA4Qw1yQMBoCOpkQAvD_BwE

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.


Solution

  • try this, If you are using selenium version > 4 , it has native shadow dom support

        IWebDriver driver = new ChromeDriver();
        driver.Navigate().GoToUrl("https://www.all4golf.de/?gclid=CjwKCAjw67ajBhAVEiwA2g_jEP_x3w6PJibWICIaE61LtLDJO_EEy85mgKRseNvZIAJpsA4Qw1yQMBoCOpkQAvD_BwE");
        IWebElement root = driver.FindElement(By.Id("usercentrics-root"));
        ISearchContext shadow = root.GetShadowRoot();
        IWebElement accept = shadow.FindElement(By.CssSelector(".sc-eDvSVe.eDuWYU")); 
        accept.Click();
        // Quit the driver
        driver.Quit();