小编典典

Selenium 在Firefox中使用过多RAM

selenium

我在Firefox中使用硒来自动执行Instagram上的某些任务。它
基本上在用户配置文件和通知页面之间来回移动,
并根据发现的内容执行任务。

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

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

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

编辑:添加了明确的条件,但没有帮助。我正在使用
Firefox的无头模式。


阅读 469

收藏
2020-06-26

共1个答案

小编典典

从Selenium开始,对Firefox 使用的RAM数量几乎没有控制权。至于你提到的浏览器客户端,即Mozilla的推移 来回用户配置文件和通知页面之间的Instagram的和 不基于它找到什么太宽泛,单任务的用例。因此, 首要的任务是打破了无限循环属于 你的用例为更小的测试。


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)
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.


Analysis

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

Firefox_RAM

但是,根据此讨论,一些用户认为使用
的内存越多越好,因为这意味着您不会浪费RAM。Firefox使用RAM来
提高其处理速度,因为应用程序数据在
RAM中的传输速度更快。


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

将Selenium升级到当前版本3.141.59。
升级GeckoDriver到GeckoDriver v0.24.0水平。
将Firefox版本升级到Firefox v65.0.2级别。
清理你的项目工作,通过你的IDE和重建仅需要依赖你的项目。
如果您的基本Web客户端版本太旧,则将其卸载并安装最新的GA和Web客户端的发行版本。
某些扩展允许您阻止此类不必要的内容,例如:

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

配置文件= webdriver.FirefoxProfile()profile.add_extension(扩展名’extension_name.xpi’)驱动程序= webdriver.Firefox(firefox_profile = profile,executable_path = r’C:\ path \ to \ geckodriver.exe’)

2020-06-26