-1

I have the following code for the button Element

enter image description here

I tried the below xpath ,unfortunately its not working .

driver.findElement(By.xpath("//button[text()='Agree to all']")).click();

Could you please help me to find the correct xpath for the Button Element?

Moni
  • 19
  • 5
  • 1
    `xpath` looks good to me. Please check if the element is inside an `iframe` or `shadow-root` element? If Url is public please attached to your question. – KunduK Apr 14 '23 at 07:23
  • Hi ,its inside shadow-root – Moni Apr 14 '23 at 07:35
  • hope https://stackoverflow.com/questions/36141681/does-anybody-know-how-to-identify-shadow-dom-web-elements-using-selenium-webdriv answers your question. – Ajeet Verma Apr 14 '23 at 07:52
  • Screenshots of the UI are great, screenshots of code or HTML are not. Please read why [a screenshot of code/HTML is a bad idea](https://meta.stackoverflow.com/questions/303812/discourage-screenshots-of-code-and-or-errors). Paste the HTML as text and properly format it instead. – JeffC Apr 14 '23 at 13:48
  • Does this answer your question? [java.lang.NullPointerException error when trying to locate a shadow element with Selenium](https://stackoverflow.com/questions/75931152/java-lang-nullpointerexception-error-when-trying-to-locate-a-shadow-element-with) – JeffC Apr 14 '23 at 13:49

1 Answers1

0

First identify the Shadow-root element and then identify the element inside it.

WebElement Shadowele = (WebElement) ((JavascriptExecutor)driver)
    .executeScript("return arguments[0].shadowRoot", driver.findElement(By.xpath("//cmm-cookie-banner[@variant='B']")));
    
Shadowele.findElement(By.xpath("//button[text()='Agree to all']")).click();
KunduK
  • 32,888
  • 5
  • 17
  • 41