Perlchaoui has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monastery

Is there a way to clear the browser cache, using Selenium::Remote::Driver module with Chrome driver. I want to clear the cache each time i need to quit the browser/driver

Thank you in advance for your wise support

  • Comment on Browser cache with Selenium::Remote::Driver

Replies are listed 'Best First'.
Re: Browser cache with Selenium::Remote::Driver
by marto (Cardinal) on Nov 17, 2018 at 17:43 UTC

    "I want to clear the cache each time i need to quit the browser"

    Most browsers have a setting specifically to do this each time you close it.

Re: Browser cache with Selenium::Remote::Driver
by bliako (Abbot) on Nov 17, 2018 at 10:52 UTC

    Can you activate the browser's "clear browser data" accessible via a "browser-url", e.g. chrome://settings/clearBrowserData and click the right buttons in there?

    Edit: For the above, just to make clear that I mean programmatically via Selenium's get() and via Selelnium's "click-button" etc.

    There is also something called "local storage" which you can clear (delete each item in there) via Selenium::Remote::Driver, but this may not cover all the "cache"

    bw, bliako

Re: Browser cache with Selenium::Remote::Driver
by harangzsolt33 (Deacon) on Nov 17, 2018 at 17:07 UTC
    You mean to erase all the history, cookies, localStorage, stored passwords, and everything on the browser? I don't think you have the privilege to do that for all websites. You might have the ability to do it on your OWN website but not for other sites.

    You can erase the cookies and the localStorage by adding the following code into the HTML output of your script:

    <SCRIPT> <!-- function EraseEverything() { function DeleteCookie(NAME) { document.cookie = NAME + "=;Expires=Th +u, 01 Jan 1970 00:00:01 GMT"; } function DeleteAllCookies() { var i, s, e, X, K = document.cookie.sp +lit(";"); for (i = 0; i < K.length; i++) { e = K[i].indexOf("="); if +(e < 0) X = ""; else { for (s = 0; K[i].charCodeAt(s) < 33; s++); X = + K[i].substring(s, e); } DeleteCookie(X); } } DeleteAllCookies(); // Empty localStorage: var SUCCESS = 0; try { localStorage.clear(); SUCCESS = 1; } catch (e) {} var storage_cleared = new Image(); storage_cleared.src = "http://www.mywebsite.com/erased.pl?s=" + SUCC +ESS + "&url=" + escape(location.href); } EraseEverything(); --> </SCRIPT> <NOSCRIPT><IMG STYLE="POSITION:ABSOLUTE; TOP:0; LEFT:0;" BORDER=0 WIDT +H=1 HEIGHT=1 SRC="http://www.mywebsite.com/erased.pl?s=2"></NOSCRIPT>

    Note: This JavaScript will give feedback to your perl script to let you know whether or not the localStorage has been erased. (Some browsers do not support this feature. And sometimes JavaScript is entirely disabled in the browser. In that case, you get a different errorcode back. So, you know exactly what's going on.

    There is also a file system and a database system feature, but these are not supported by most browsers, and I am not familiar with how they operate. Most websites use cookies and localStorage, because these two are supported by nearly every web browser out there.

    I do not know how to erase the form history, passwords, and history from the browser. You could tie the form names to the current time, so that way every second when the user refreshes the page, the form data gets written into a different temporary name, and it's never the same. So, the user will never see the previous suggestions in the browser. Example:

    <SCRIPT> <!-- NOW = (new Date()).getTime(); document.write("<FORM NAME=MAIN METHOD=POST ACTION='http://www.mywebsi +te.com/getname.cgi'>Please enter your first name: <INPUT TYPE=TEXT SI +ZE=20 NAME=FIRSTNAME" + NOW + "> <INPUT TYPE=SUBMIT></FORM>"); --> </SCRIPT>

    The browser remembers the form values based on their name, but if the name changes every time, then it won't show any suggestions ever.

      "You mean to erase all the history, cookies, localStorage, stored passwords, and everything on the browser? I don't think you have the privilege to do that for all websites. You might have the ability to do it on your OWN website but not for other sites."

      You think web browsers prevent people clearing such content for sites you they don't actually own? How would that work?

      Update: s/you/they/