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.
Join the DZone community and get the full member experience.
Join For FreeWhen 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.
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.
I optimized the code and came up with a shorter version as below:
x
public static void ClearBrowserCache(IWebDriver webDriver)
{
webDriver.Navigate().GoToUrl("chrome://settings/clearBrowserData");
// begin identify clear data button via nested Shadow Dom elements
// get 1st parent
IWebElement shadowRoot1 = (webDriver.ByCssSelector("settings-ui")).getShadowRootElement();
IWebElement shadowRoot2 = (shadowRoot1.FindElementByCss("settings-main")).getShadowRootElement();
IWebElement shadowRoot3 = (shadowRoot2.FindElementByCss("settings-basic-page")).getShadowRootElement();
IWebElement shadowRoot4 = (shadowRoot3.FindElementByCss("settings-section > settings-privacy-page")).getShadowRootElement();
IWebElement shadowRoot5 = (shadowRoot4.FindElementByCss("settings-clear-browsing-data-dialog")).getShadowRootElement();
IWebElement root6 = shadowRoot5.FindElementByCss("#clearBrowsingDataDialog");
SelectTimeRangeAll(root6);
IWebElement clearDataButton = root6.FindElement(By.CssSelector("#clearBrowsingDataConfirm"));
clearDataButton.Click(); // click that hard to reach button!
}
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:
xxxxxxxxxx
public static IWebElement ByCssSelector(this IWebDriver webDriver,string locator)
{
return webDriver.FindElement(By.CssSelector(locator));
}
public static IWebElement FindElementByCss(this IWebElement element,string locator)
{
int elapsed = 0, timeout = 1000, pollingInterval = 200;
do
{
try
{
IWebElement element2 = element.FindElement(By.CssSelector(locator));
if (element2 != null)
return element2;
}
catch (NoSuchElementException)
{
Thread.Sleep(400);
continue;
}
finally
{
elapsed += pollingInterval;
}
} while (elapsed < timeout);
return null;
}
The following function sets the Time range to AllTime. We can change the SelectByValue
call to select other values in the list.
xxxxxxxxxx
private static void SelectTimeRangeAll(IWebElement root)
{
IWebElement root2 = (root.FindElementByCss("iron-pages#tabs")).FindElementByCss("settings-dropdown-menu#clearFromBasic") ;
IWebElement shadowRoot2 = root2.getShadowRootElement();
IWebElement ddnTimeRange = shadowRoot2.FindElement(By.CssSelector("select#dropdownMenu"));
new SelectElement(ddnTimeRange).SelectByValue("4");
}
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:
xxxxxxxxxx
[SetUp]
public void Setup()
{
driver = BrowserFactory.GetChromeBrowser();
BrowserHelper.ClearBrowserCache(driver);
driver.Navigate().GoToUrl("https://trello.com/");
}
Published at DZone with permission of Suparna Shaligram. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments