Selenium using too much RAM with Firefox

情到浓时终转凉″ 提交于 2019-11-28 13:00:34

To start with Selenium have very little control over the amount of RAM used by Firefox. As you mentioned the Browser Client i.e. Mozilla goes back and forth between user profiles and notifications page on Instagram and does tasks based on what it finds is too broad as a single usecase. So, the first and foremost task would be to break up the infinite loop pertaining to your usecase into smaller Tests.


time.sleep()

Inducing time.sleep() vitually puts a blanket over the underlying issue. However while using Selenium and WebDriver for Automation using time.sleep(secs) without any specific condition to achieve defeats the purpose of Automation and should be avoided at any cost. As per the documentation:

time.sleep(secs) suspends the execution of the current thread for the given number of seconds. The argument may be a floating point number to indicate a more precise sleep time. The actual suspension time may be less than that requested because any caught signal will terminate the sleep() following execution of that signal’s catching routine. Also, the suspension time may be longer than requested by an arbitrary amount because of the scheduling of other activity in the system.

You can find a detailed discussion in How to sleep webdriver in python for milliseconds


Analysis

There were previous instances when Firefox consumed about 80% of the RAM.

However as per this discussion some of the users feels that the more memory is used the better because it means you don't have RAM wasted. Firefox uses RAM to make its processes faster since application data is transferred much faster in RAM.


Solution

You can implement either/all of the generic/specific steps as follows:

  • Upgrade Selenium to current levels Version 3.141.59.
  • Upgrade GeckoDriver to GeckoDriver v0.24.0 level.
  • Upgrade Firefox version to Firefox v65.0.2 levels.
  • Clean your Project Workspace through your IDE and Rebuild your project with required dependencies only.
  • If your base Web Client version is too old, then uninstall it and install a recent GA and released version of Web Client.
  • Some extensions allow you to block such unnecessary content, as an example:

    • uBlock Origin allows you to hide ads on websites.
    • NoScript allows you to selectively enable and disable all scripts running on websites.
    • To open the Firefox client with an extension you can download the extension i.e. the XPI file from https://addons.mozilla.org and use the add_extension(extension='webdriver.xpi') method to add the extension in a FirefoxProfile as follows:

      from selenium import webdriver
      
      profile = webdriver.FirefoxProfile() 
      profile.add_extension(extension='extension_name.xpi')
      driver = webdriver.Firefox(firefox_profile=profile, executable_path=r'C:\path\to\geckodriver.exe') 
      
  • If your Tests doesn't requires the CSS you can disable the CSS following the this discussion.

  • Use Explicit Waits or Implicit Waits.
  • Use driver.quit() to close all the browser windows and terminate the WebDriver session because if you do not use quit() at the end of the program, the WebDriver session will not be closed properly and the files will not be cleared off memory. And this may result in memory leak errors.

Creating new firefox profile and use it every time while running test cases in Firefox shall eventually increase the performance of execution as without doing so always new profile would be created and caching information would be done there and if driver.quit does not get called somehow before failure then in this case, every time we end up having new profiles created with some cached information which would be consuming memory.

// ------------ Creating a new firefox profile -------------------

1. If Firefox is open, close Firefox.
2. Press Windows +R on the keyboard. A Run dialog will open.
3. In the Run dialog box, type in firefox.exe -P
Note: You can use -P or -ProfileManager(either one should work).
4. Click OK.
5. Create a new profile and sets its location to the RAM Drive.

// ----------- Associating Firefox profile -------------------

ProfilesIni profile = new ProfilesIni();
FirefoxProfile myprofile = profile.getProfile("automation_profile");
WebDriver driver = new FirefoxDriver(myprofile);

Please share execution performance with community if you plan to implement this way.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!