Forum Discussion

nwalimbe's avatar
nwalimbe
Occasional Contributor
6 years ago

How to clear browser cache in firefox and chrome

Before I login to the application, I want the ability to clear the cache. This is because the passwords and urls etc are stored in cache and before I execute any tests, I need to ensure that the cache is cleared. I am writing in python script. Is it possible? If yes, how?

8 Replies

    • nwalimbe's avatar
      nwalimbe
      Occasional Contributor

      Thanks a lot shankar_r. However this seems to be in a language I do not understand. Could you pls. convert it into python or give me some pointers. I tried googling it, but looks like I am making mistakes here and there.

       

      Thanks,

      • nwalimbe's avatar
        nwalimbe
        Occasional Contributor

        This is the code, I am trying to use:

         

        def ClearAllChromeStoredInfo():

        objFSO = CreateObject("Scripting.FileSystemObject")
        strAppDataFolder = aqEnvironment.GetEnvironmentVariable(("LocalAppData") & "\Google\Chrome\User Data\Default")
        FilesInFolder = objFSO.GetFolder(strAppDataFolder).Files
        Log.Message(FilesInFolder)
        FoldersInFolder = objFSO.GetFolder(strAppDataFolder).SubFolders
        for Folder in FoldersInFolder:
        if UCase(Folder.Name) = "EXTENSIONS" and LCase(Folder.Name) = "EXTENSION STATE"
        Folder.Delete True
        break
        For File In range (FilesInFolder)
        If Not UCase(File.Name) = "PREFERENCES"
        File.Delete
        break

         

        The error I am getting is attached.

    • shankar_r's avatar
      shankar_r
      Community Hero

      AlexKaras I am admiring wherever i see old threads there i see you in comments which insist me how much you are contributing for the peoples in the community to get away from automation headaches. :smileyhappy:

  • Hi!

     

    There is a workaround using Keys() method of browser Page object. Sorry, I'm not familiar with Python but in JScript it looks like following:

     

    var browser = Sys.Find('ProcessName', 'chrome');

    var page = browser.Page('http://yourpage.url/');

    page.Keys("!^[Del]"); // Ctrl + Shift + Delete keys combination opens clear browser data dialog
    SleepSeconds(2);

    browser.Page('chrome://settings/clearBrowserData').Keys("[Enter]"); // hit Enter in clear browser data dialog
    SleepSeconds(2);
    browser.Page('chrome://settings/').Close(); // close settings page after clearing browsing data

    page.Keys('[F5]');

  • ucuber's avatar
    ucuber
    Occasional Contributor

    Aside the fact, that the question is a little bit older, here a solution in python for chrome I made for my tests. If you have some ideas to make it more elegant or effective or if you see errors, feel free to refactor it

     

    Remark: on my system all the chrome stuff is in Roaming, so I use AppData instead of LocalAppData

     

    def clear_stored_info_chrome():
        excluded_items = [item.upper() for item in [
                              r"Preferences",
                              r"Origin Bound",
                              r"Extension"
                            ] 
                          ]
        found_items = [item.upper() for item in glob.glob(
                          os.path.join(
                            aqEnvironment.GetEnvironmentVariable("AppData"),
                            r"Google\Chrome\User Data\Default\*"
                          )
                        )
                      ]
        for item in found_items:
          is_excluded = [True for tag in excluded_items if tag in item]
          if not is_excluded:
            try:
              if os.path.isdir(item):
                 shutil.rmtree(item, ignore_errors=True)
              else:
                  os.remove(item)
            except BaseException as e: # I ignore errors of any kind, just a remark to Log
              Log.Message("clear chrome stored info: {} - {}".format(item, e))