DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • 8 Must-Have Project Reports You Can Use Today
  • Why Round-Robin Won't Save You: Load Balancing Challenges in Data Streaming Services With Heterogeneous Traffic
  • Good Data, Bad Metric: A Mutation Testing Pattern for Analytics Engineering
  • A System Cannot Protect What It Does Not Understand

Trending

  • Why Good Models Fail After Deployment
  • Bringing Intelligence Closer to the Source: Why Real-Time Processing is the Heart of Edge AI
  • Introduction to Retrieval Augmented Generation (RAG)
  • LLM Integration in Enterprise Applications: A Practical Guide
  1. DZone
  2. Data Engineering
  3. Data
  4. Using Selenium To Clear Browsing Data In Chrome

Using Selenium To Clear Browsing Data In Chrome

When working with pages in several automating projects I noticed that the fields like login ID, search fields, etc., are getting saved on the page.

By 
Suparna Shaligram user avatar
Suparna Shaligram
·
May. 04, 21 · Tutorial
Likes (2)
Comment
Save
Tweet
Share
17.3K Views

Join the DZone community and get the full member experience.

Join For Free

When working with pages in several automating projects I noticed that the fields like login ID, search fields, etc., are getting saved on the page. Thus, when my Selenium test runs, I need to put additional code to clear those fields if those are populated. I needed a way to clear the browsing data through my test before I went to the page.

Clear Browsing History UI

I attempted a few solutions from here and here. However, these worked on older versions of the Chrome browser. Next, I tried out the solution from here. It worked in part but I needed to also set the time range as AllTime.

A longer version of the final completed code is here. It basically traverses through the DOM which is tricky because of the nested shadow roots. This version is easy to understand but it is long and it's a bit flaky as well.Screenshot of Code

I optimized the code and came up with a shorter version as below:

C#
 




x


 
1
public static void ClearBrowserCache(IWebDriver webDriver)
2
        {
3
            webDriver.Navigate().GoToUrl("chrome://settings/clearBrowserData");
4

          
5
            // begin identify clear data button via nested Shadow Dom elements
6
            // get 1st parent
7
            IWebElement shadowRoot1 = (webDriver.ByCssSelector("settings-ui")).getShadowRootElement();
8
            IWebElement shadowRoot2 = (shadowRoot1.FindElementByCss("settings-main")).getShadowRootElement();
9
            IWebElement shadowRoot3 = (shadowRoot2.FindElementByCss("settings-basic-page")).getShadowRootElement();
10
          IWebElement shadowRoot4 = (shadowRoot3.FindElementByCss("settings-section > settings-privacy-page")).getShadowRootElement();
11
            IWebElement shadowRoot5 = (shadowRoot4.FindElementByCss("settings-clear-browsing-data-dialog")).getShadowRootElement();  
12
            IWebElement root6 = shadowRoot5.FindElementByCss("#clearBrowsingDataDialog");
13
            SelectTimeRangeAll(root6);
14
            IWebElement clearDataButton = root6.FindElement(By.CssSelector("#clearBrowsingDataConfirm"));
15
            clearDataButton.Click(); // click that hard to reach button!
16
         
17
        }



In the above code, we bring up the ClearBrowserData modal and then start getting the shadow root elements. Our Clear Data button is nested in the shadow root structure so a normal XPath will not be able to find it. I have also added a call to set the TimeRange as well. The extension methods written for IWebElement and IWebDriver used above are as below:

C#
 




xxxxxxxxxx
1
29


 
1
  public static IWebElement ByCssSelector(this IWebDriver webDriver,string locator)
2
        {
3
            return webDriver.FindElement(By.CssSelector(locator));
4
        }
5

          
6
 public static IWebElement FindElementByCss(this IWebElement element,string locator)
7
        {
8
            int elapsed = 0, timeout = 1000, pollingInterval = 200;
9
            do
10
            {
11
                try
12
                {
13
                    IWebElement element2 = element.FindElement(By.CssSelector(locator));
14
                    if (element2 != null)
15
                        return element2;
16
                }
17
                catch (NoSuchElementException)
18
                {
19
                    Thread.Sleep(400);
20
                    continue;
21
                }
22
                finally
23
                {
24
                    elapsed += pollingInterval;
25
                }
26

          
27
            } while (elapsed < timeout);          
28
            return null;
29
        }



The following function sets the Time range to AllTime. We can change the SelectByValue call to select other values in the list.

C#
 




xxxxxxxxxx
1


 
1
private static void SelectTimeRangeAll(IWebElement root)
2
        {
3
            IWebElement root2 = (root.FindElementByCss("iron-pages#tabs")).FindElementByCss("settings-dropdown-menu#clearFromBasic") ;            
4
            IWebElement shadowRoot2 = root2.getShadowRootElement();
5
            IWebElement ddnTimeRange = shadowRoot2.FindElement(By.CssSelector("select#dropdownMenu"));
6
            new SelectElement(ddnTimeRange).SelectByValue("4");
7
        }



I put the ClearBrowserCache method in a BrowserHelper.cs method in my framework. These methods work with Chrome 90.0.4430.85 (Official Build) (64-bit).

Call the above ClearBrowserCache method in your setup method as below:

Java
 




xxxxxxxxxx
1


 
1
 [SetUp]
2
        public void Setup()
3
        {
4
            driver = BrowserFactory.GetChromeBrowser();
5
            BrowserHelper.ClearBrowserCache(driver);
6
            driver.Navigate().GoToUrl("https://trello.com/");           
7
        }



Clear (Unix) Data (computing)

Published at DZone with permission of Suparna Shaligram. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • 8 Must-Have Project Reports You Can Use Today
  • Why Round-Robin Won't Save You: Load Balancing Challenges in Data Streaming Services With Heterogeneous Traffic
  • Good Data, Bad Metric: A Mutation Testing Pattern for Analytics Engineering
  • A System Cannot Protect What It Does Not Understand

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook