硒在Firefox中使用过多RAM [英] Selenium using too much RAM with Firefox

查看:74
本文介绍了硒在Firefox中使用过多RAM的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Firefox上使用硒来自动执行Instagram上的某些任务.它基本上是在用户个人资料和通知页面之间来回移动,并根据发现的内容执行任务.

I am using selenium with Firefox to automate some tasks on Instagram. It basically goes back and forth between user profiles and notifications page and does tasks based on what it finds.

它具有一个无限循环,可确保任务继续进行.我每隔几步就有一次sleep()函数,但是内存使用量一直在增加.我在Python中有这样的东西:

It has one infinite loop that makes sure that the task keeps on going. I have sleep() function every few steps but the memory usage keeps increasing. I have something like this in Python:

while(True):
    expected_conditions()
    ...doTask()
    driver.back()
    expected_conditions()
    ...doAnotherTask()
    driver.forward()
    expected_conditions()

我从不关闭驱动程序,因为这会减慢程序的速度,因为它要处理的查询很多.有什么方法可以避免在不关闭或退出驱动程序的情况下增加内存使用量的时间?

I never close the driver because that will slow down the program by a lot as it has a lot of queries to process. Is there any way to keep the memory usage from increasing overtime without closing or quitting the driver?

添加了明确的条件,但也无济于事.我正在使用Firefox的无头模式.

Added explicit conditions but that did not help either. I am using headless mode of Firefox.

推荐答案

开头对 Firefox使用的 RAM 的数量几乎没有控制权.正如您提到的 Browser Client ,即 Mozilla Instagram 上的用户配置文件和通知页面之间来回切换,并根据发现的内容执行任务作为单个用例太宽.因此,首要任务是将与您的用例有关的无限循环分解为较小的 Tests .

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()实际上掩盖了潜在的问题.但是,在使用 Selenium 执行测试自动化框架,在没有任何特定条件的情况下使用time.sleep() 会破坏自动化的目的,因此应不惜一切代价避免使用.根据文档:

Inducing time.sleep() virtually puts a blanket over the underlying issue. However while using Selenium and WebDriver to execute tests through your Automation Framework, using time.sleep() without any specific condition defeats the purpose of automation and should be avoided at any cost. As per the documentation:

time.sleep(secs)暂停执行在给定的秒数内当前线程数.该参数可以是浮点数,以指示更精确的睡眠时间.实际的暂停时间可能少于请求的暂停时间,因为任何捕获到的信号都会在执行该信号的捕获例程后终止sleep().另外,由于系统中其他活动的安排,暂停时间可能比请求的时间长任意数量.

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.

您可以在如何找到详细讨论在python中休眠webdriver数毫秒

在以前的情况下, Firefox 消耗了大约80%的RAM.

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

但是根据此讨论,有些用户认为使用的内存越多越好,因为这意味着您不会浪费RAM. Firefox使用RAM来提高其处理速度,因为应用程序数据在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.

您可以执行以下一个或所有通用/特定步骤:

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

  • 升级到当前级别版本3.141.59 .
  • GeckoDriver 升级到 GeckoDriver v0.24.0 级别. /li>
  • Firefox 版本升级到 Firefox v65.0.2 级别.
  • 通过 IDE
  • 清理您的项目工作区重建仅具有所需依赖项的项目.
  • 如果您的基本 Web客户端版本太旧,则将其卸载并安装最新版本的 Web客户端 GA.
  • 某些扩展程序使您可以阻止此类不必要的内容,例如:

  • 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') 

如果您的 Tests 不需要 CSS ,则可以按照

If your Tests doesn't requires the CSS you can disable the CSS following the this discussion.

这篇关于硒在Firefox中使用过多RAM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆