Ultimately, this question comes down to "How to find any element locator for a website."
To do this, you can use the Developer Tools in Chrome (or any modern web browser).
- Open a website and locate the element you want (in this case, a button).
- Right-click on the element and select "Inspect". The dev tools will open with the Elements tab open. The web element you selected will be highlighted in the Elements tab.
- Right-click in the Elements tab on the highlighted DOM node.
- Select Copy. A submenu will open.
- Select Copy Xpath (or any other option like Copy Selector for CSS selectors or Copy Full Xpath).
When I copy Xpath, I get: //*[@id="app"]/div[1]/cmm-cookie-banner//div/div/div[2]/cmm-buttons-wrapper/div/div/wb7-button[2]
When I copy Selector, I get: div > div > div.cmm-cookie-banner__content > cmm-buttons-wrapper > div > div > wb7-button.button.button--accept-all.wb-button.hydrated
When I copy Full Xpath, I get: /html/body/div/div[1]/cmm-cookie-banner//div/div/div[2]/cmm-buttons-wrapper/div/div/wb7-button[2]
We don't know whether the OP has tried any of those or not. And we don't know what version of Selenium the OP is using or the browser version. However, in Selenium 4 and higher, and using Chrome 96 and higher, they added a new method WebElement.getShadowRoot()
The Java example they use is:
WebElement shadowHost = driver.findElement(By.cssSelector("#shadow_host"));
SearchContext shadowRoot = shadowHost.getShadowRoot();
WebElement shadowContent = shadowRoot.findElement(By.cssSelector("#shadow_content"));
In essence, you cannot use Xpath to locate an element in the shadow root. You can only use a CSS Selector. Using this example code from Seleniem.dev, you can now just change the CSS Selectors.
What are the #shadow_host
and #shadow_content
? The #shadow_host
returns a reference to a regular DOM element to use, which is not in the shadow. The #shadow_content
is the element inside the shadow-root.