1

I am trying to clear browser cache, for which i need to click on clear data button of browser setting popup, but, i am not able to write xpath for the button on chrome browser

i have tried inspecting the element to find out if the button is on a iframe but its not in iframe, so i have decided to try it with an with out iframe snippet, either of ways the element is not traces out in dom

    public void clearBrowserCache() throws InterruptedException{
    driver.get("chrome://settings/clearBrowserData");
    Thread.sleep(2000);
    System.out.println(driver.getWindowHandles());
    String windowIds=driver.getWindowHandle();

   // driver.switchTo().frame(windowIds);
      driver.findElement(By.cssSelector(
      [id=clearBrowsingDataConfirm]")).click();

    }

Expected is that i should be able to click on the clear data button Actual is that i am not able to find out the xpath for of the emlement

Rajaji
  • 133
  • 2
  • 14
  • 1
    You are dealing with shadow root element. See this question: https://stackoverflow.com/questions/37384458/how-to-handle-elements-inside-shadow-dom-from-selenium. – Mate Mrše May 29 '19 at 13:47
  • Are you sure you are switching to the correct Handle? If I were you, I'd get the Handle before the 'clear browsing data' and make sure you're not switching to the wrong Handle. What does it say when you printed out the window handles? – DynamicSwarmi May 29 '19 at 14:24
  • @DynamicSwarmi i have tried printing the getWindowHandles which is printing only one window ID, more over when inspected the DOM is not having any iframes – Rajaji May 30 '19 at 15:00

2 Answers2

0

Depending on which version of chrome you are using, this could work:

driver.findElement(By.cssSelector("* /deep/ #clearBrowsingDataConfirm")).click();

However the /deep/ combinator is deprecated, so it may not work on newer Chrome's versions.

nunoq
  • 550
  • 2
  • 7
  • Hi you are right, my latest version of chrome [74] is not working with this "* /deep/ #clearBrowsingDataConfirm" – Rajaji May 30 '19 at 03:24
0

I answered how to reach inside the Shadow DOM in an other question.

You can read the whole thing at the link, but the basics are you create a "starting point" WebElement at the Shadow DOM via JavaScript, then all future look-ups reference it:

WebElement button = startingPoint.findElement(By.cssSelector("..."));
MivaScott
  • 1,763
  • 1
  • 12
  • 30