1

I am working with C# and Selenium and am trying to press this button.

enter image description here

I tried a lot of ways to click on this butten. But Selenium seem not able to find the element. My latest try is

IWebElement clickableButton = webDriver.FindElement(By.XPath("//*[@id='uc - center - container']/div[2]/div/div/div/div/button[1]"));
clickableButton.Click();

However, all tries lead to the fault:

OpenQA.Selenium.NoSuchElementException : no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id='uc - center - container']/div[2]/div/div/div/div/button[1]"}

How can I press this button?

Let me know if more/further information is helpful to answer this question.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Phill
  • 35
  • 5

3 Answers3

1

The <button> element is within a #shadow-root (open)

shadow-root


Solution

To click on the <button> element you have to use shadowRoot.querySelector() and you can use the following Locator Strategy:

new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable((IWebElement)((IJavaScriptExecutor)driver).ExecuteScript("return document.querySelector('div#usercentrics-root').shadowRoot.querySelector('button[data-testid=\"uc-customize-button\"]')"))).Click();
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • It brought me one step further. But still it looks like the element does not get located. However I am still glad, that I now understand, that Selenium seems to have some issues to locate a shadow-root element. I get the error: Test method WithDotNet.UnitTest1.TestMethod1 threw exception: OpenQA.Selenium.WebDriverTimeoutException: Timed out after 20 seconds --> I think it is still not finding the element – Phill Aug 05 '22 at 15:58
  • Check in the `Development Tools` -> `Console` if executing the command `return document.querySelector('div#usercentrics-root').shadowRoot.querySelector('button[data-testid=\"uc-customize-button\"]')` identifies the element uniquely. – undetected Selenium Aug 05 '22 at 16:02
  • I get the message "Failed to load resource: the server responded with a status of 401 ()" However the website does load, I can see the butten, and I can also press is manually. – Phill Aug 05 '22 at 16:44
  • _I can see the butten, and I can also press is manually_: That wasn't my question either. Please reread my question again. – undetected Selenium Aug 05 '22 at 19:29
  • Without seeing the actual shadowRoot I think we have reached to a dead-lock now – undetected Selenium Aug 05 '22 at 19:48
  • Sorry, I was not sure how to answer your question. I think I get it now. Hopefully. – Phill Aug 06 '22 at 09:08
  • I get the error(s) "JSError t=1659776828184&level=ERRO&source=onerror&fileName=https%3A%..." and "VM2917:1 Uncaught SyntaxError: Illegal return statement" – Phill Aug 06 '22 at 09:13
  • 1
    an update: the following works in the developer tools console: document.querySelector("#usercentrics-root").shadowRoot.querySelector("#uc-center-container > div.sc-bYoBSM.jvautU > div > div > div > div > button.sc-gsDKAQ.iVBeYE").click() – Phill Aug 06 '22 at 16:01
  • @Phill Honestly, we can't improve our answer as we were unable to test our answer before publishing. That's why on SO we recommend the text based HTML instead of a snapshot. – undetected Selenium Aug 06 '22 at 16:08
  • Totally understandable. I appreciate all the effort. I really do. However I do not know how to copy the HTML code. – Phill Aug 06 '22 at 17:06
1

First of all, I found a documentation in git saying this is a current known issue/bug/white-spot in Selenium for C# (Can't find the link anymore).

Anyhow, I was able to click the button with Java.

JavascriptExecutor jse =(JavascriptExecutor)driver;
WebElement element = (WebElement)jse.executeScript("return document.querySelector(\"#usercentrics-root\").shadowRoot.querySelector(\"#uc-center-container > div.sc-bYoBSM.jvautU > div > div > div > button.sc-gsDKAQ.iVBeYE\")");
element.click();
Twenty
  • 5,234
  • 4
  • 32
  • 67
Phill
  • 35
  • 5
0

This is code without JavaScript

IWebElement shadowHost = Driver.FindElement(By.Id("usercentrics-root"));
ISearchContext shadowRoot = shadowHost.GetShadowRoot();
IWebElement clickableButton = shadowRoot.FindElement(By.CssSelector("button[data-testid='uc-customize-button']"));
clickableButton.Click();

In shadow root you can find any element with CssSelector only.

Twenty
  • 5,234
  • 4
  • 32
  • 67