Selenium 在 Firefox 中使用过多的 RAM
Selenium using too much RAM with Firefox
我在 Firefox 中使用 selenium 来自动执行 Instagram 上的一些任务。它基本上在用户配置文件和通知页面之间来回切换,并根据发现的内容执行任务。
它有一个无限循环,确保任务继续进行。我每隔几步就有 sleep() 函数,但内存使用量不断增加。我在 Python:
中有这样的东西
while(True):
expected_conditions()
...doTask()
driver.back()
expected_conditions()
...doAnotherTask()
driver.forward()
expected_conditions()
我从不关闭驱动程序,因为这会大大降低程序速度,因为它有很多查询要处理。有什么方法可以在不关闭或退出驱动程序的情况下防止内存使用量超时增加?
编辑:添加了明确的条件,但这也无济于事。我正在使用 Firefox 的无头模式。
- 使用显式等待或隐式等待。
- 用driver.quit()关闭所有
浏览器 windows 并终止 WebDriver 会话,因为如果
你不在程序结束时使用 quit() ,WebDriver
session 不会正常关闭,文件不会被清除
关闭内存。这可能会导致内存泄漏错误。
创建新的 firefox 配置文件并每次都使用它 运行 Firefox 中的测试用例最终会提高执行性能,因为如果不这样做,总是会创建新的配置文件并在那里完成缓存信息,如果driver.quit 在失败之前不会以某种方式被调用,那么在这种情况下,每次我们最终都会使用一些缓存信息创建新的配置文件,这会消耗内存。
// ---------- 创建一个新的 firefox 配置文件 ------------------
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.
// ---------- 关联 Firefox 配置文件 ------------------
ProfilesIni profile = new ProfilesIni();
FirefoxProfile myprofile = profile.getProfile("automation_profile");
WebDriver driver = new FirefoxDriver(myprofile);
如果您打算以这种方式实施,请与社区分享执行绩效。
从 Selenium 开始,对 Firefox[=95= 使用的 RAM 的数量几乎没有控制].正如您提到的 Browser Client 即 Mozilla 在 Instagram 上的用户配置文件和通知页面之间来回切换并根据它发现的内容作为单个 用例 来执行任务。因此,首要任务是将与您的 用例 相关的 无限循环 分解为更小的 测试 .
time.sleep()
诱导 time.sleep()
实际上掩盖了根本问题。然而,在使用 Selenium 和 to execute tests through your Automation Framework 时,在没有任何特定条件的情况下使用 time.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.
您可以在
中找到详细的讨论
分析
以前有 Firefox 消耗大约 80% RAM 的情况。
然而,根据 this discussion 一些用户认为 使用的内存越多越好,因为这意味着您不会浪费 RAM。 Firefox 使用 RAM 来加快其进程,因为应用程序数据在 RAM 中的传输速度要快得多。
解决方案
您可以执行 generic/specific 步骤中的 either/all,如下所示:
- 将 Selenium 升级到当前水平 Version 3.141.59。
- 将 GeckoDriver 升级到 GeckoDriver v0.24.0 级别。
- 将 Firefox 版本升级到 Firefox v65.0.2 级别。
- 通过 IDE 和 [=51] 清理 您的 项目工作区 =]重建你的项目只需要依赖。
- 如果您的基本 Web 客户端 版本太旧,则卸载它并安装最新的 GA 和发布版本的 Web 客户端。
一些扩展允许您阻止这些不必要的内容,例如:
- uBlock Origin 允许您隐藏网站上的广告。
- NoScript 允许您有选择地启用和禁用网站上的所有脚本 运行。
要打开带有扩展程序的 Firefox 客户端,您可以下载扩展程序,即来自 https://addons.mozilla.org and use the add_extension(extension='webdriver.xpi') 方法的 XPI
文件以添加FirefoxProfile 中的扩展如下:
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')
如果您的测试不需要CSS,您可以禁用CSS在this discussion.
之后
目前还没有解决这个问题。
我建议你使用 driver.close() 方法。
我也在为 RAM 问题而苦苦挣扎,我所做的是计算循环次数,当循环次数达到一定数量(对我来说是 200)时,我调用 driver.close() 然后启动驱动程序再次返回并重置计数。
这样我就不需要在每次执行循环时都关闭驱动程序,并且对性能的影响也较小。
试试这个。也许它对你的情况也有帮助。
好吧,这是我几天来一直遇到的严重问题。但我找到了解决办法。您可以添加一些标志来优化您的内存使用。
options = Options()
options.add_argument("start-maximized")
options.add_argument("disable-infobars")
options.add_argument("--disable-extensions")
options.add_argument('--no-sandbox')
options.add_argument('--disable-application-cache')
options.add_argument('--disable-gpu')
options.add_argument("--disable-dev-shm-usage")
这些是我添加的标志。在我添加标志之前,RAM 使用量在超过 4GB(我的机器是 8GB)后一直在增加,我的机器卡住了。在我添加这些标志后,内存使用量没有超过 500MB。正如 DebanjanB 的回答,如果你 运行 for loop
或 while loop
试图在每次执行后让一些秒睡眠,它会给你一些时间来杀死未使用的线程。
我在 Firefox 中使用 selenium 来自动执行 Instagram 上的一些任务。它基本上在用户配置文件和通知页面之间来回切换,并根据发现的内容执行任务。
它有一个无限循环,确保任务继续进行。我每隔几步就有 sleep() 函数,但内存使用量不断增加。我在 Python:
中有这样的东西while(True):
expected_conditions()
...doTask()
driver.back()
expected_conditions()
...doAnotherTask()
driver.forward()
expected_conditions()
我从不关闭驱动程序,因为这会大大降低程序速度,因为它有很多查询要处理。有什么方法可以在不关闭或退出驱动程序的情况下防止内存使用量超时增加?
编辑:添加了明确的条件,但这也无济于事。我正在使用 Firefox 的无头模式。
- 使用显式等待或隐式等待。
- 用driver.quit()关闭所有 浏览器 windows 并终止 WebDriver 会话,因为如果 你不在程序结束时使用 quit() ,WebDriver session 不会正常关闭,文件不会被清除 关闭内存。这可能会导致内存泄漏错误。
创建新的 firefox 配置文件并每次都使用它 运行 Firefox 中的测试用例最终会提高执行性能,因为如果不这样做,总是会创建新的配置文件并在那里完成缓存信息,如果driver.quit 在失败之前不会以某种方式被调用,那么在这种情况下,每次我们最终都会使用一些缓存信息创建新的配置文件,这会消耗内存。
// ---------- 创建一个新的 firefox 配置文件 ------------------
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.
// ---------- 关联 Firefox 配置文件 ------------------
ProfilesIni profile = new ProfilesIni();
FirefoxProfile myprofile = profile.getProfile("automation_profile");
WebDriver driver = new FirefoxDriver(myprofile);
如果您打算以这种方式实施,请与社区分享执行绩效。
从 Selenium 开始,对 Firefox[=95= 使用的 RAM 的数量几乎没有控制].正如您提到的 Browser Client 即 Mozilla 在 Instagram 上的用户配置文件和通知页面之间来回切换并根据它发现的内容作为单个 用例 来执行任务。因此,首要任务是将与您的 用例 相关的 无限循环 分解为更小的 测试 .
time.sleep()
诱导 time.sleep()
实际上掩盖了根本问题。然而,在使用 Selenium 和 time.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.
您可以在
分析
以前有 Firefox 消耗大约 80% RAM 的情况。
然而,根据 this discussion 一些用户认为 使用的内存越多越好,因为这意味着您不会浪费 RAM。 Firefox 使用 RAM 来加快其进程,因为应用程序数据在 RAM 中的传输速度要快得多。
解决方案
您可以执行 generic/specific 步骤中的 either/all,如下所示:
- 将 Selenium 升级到当前水平 Version 3.141.59。
- 将 GeckoDriver 升级到 GeckoDriver v0.24.0 级别。
- 将 Firefox 版本升级到 Firefox v65.0.2 级别。
- 通过 IDE 和 [=51] 清理 您的 项目工作区 =]重建你的项目只需要依赖。
- 如果您的基本 Web 客户端 版本太旧,则卸载它并安装最新的 GA 和发布版本的 Web 客户端。
一些扩展允许您阻止这些不必要的内容,例如:
- uBlock Origin 允许您隐藏网站上的广告。
- NoScript 允许您有选择地启用和禁用网站上的所有脚本 运行。
要打开带有扩展程序的 Firefox 客户端,您可以下载扩展程序,即来自 https://addons.mozilla.org and use the add_extension(extension='webdriver.xpi') 方法的
XPI
文件以添加FirefoxProfile 中的扩展如下: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')
如果您的测试不需要CSS,您可以禁用CSS在this discussion.
之后
目前还没有解决这个问题。 我建议你使用 driver.close() 方法。 我也在为 RAM 问题而苦苦挣扎,我所做的是计算循环次数,当循环次数达到一定数量(对我来说是 200)时,我调用 driver.close() 然后启动驱动程序再次返回并重置计数。 这样我就不需要在每次执行循环时都关闭驱动程序,并且对性能的影响也较小。 试试这个。也许它对你的情况也有帮助。
好吧,这是我几天来一直遇到的严重问题。但我找到了解决办法。您可以添加一些标志来优化您的内存使用。
options = Options()
options.add_argument("start-maximized")
options.add_argument("disable-infobars")
options.add_argument("--disable-extensions")
options.add_argument('--no-sandbox')
options.add_argument('--disable-application-cache')
options.add_argument('--disable-gpu')
options.add_argument("--disable-dev-shm-usage")
这些是我添加的标志。在我添加标志之前,RAM 使用量在超过 4GB(我的机器是 8GB)后一直在增加,我的机器卡住了。在我添加这些标志后,内存使用量没有超过 500MB。正如 DebanjanB 的回答,如果你 运行 for loop
或 while loop
试图在每次执行后让一些秒睡眠,它会给你一些时间来杀死未使用的线程。