在通过Selenium和Python进行首页查询时,ChromeDriver的速度比GeckoDriver的速度慢 [英] ChromeDriver is too slower than GeckoDriver on the first page query through Selenium and Python
问题描述
我有最新版本的驱动程序(chromedriver = 2.44.609551),硒包(3.141.0)和(无头chrome = 70.0.3538.110).(在Windows上)
I have the latest version of the drivers (chromedriver=2.44.609551), selenium package(3.141.0) and the (headless chrome=70.0.3538.110). (on windows)
我正在使用浏览器打开多个窗口.使用firefox,我的脚本总是很快.但是在chrome上,切换到窗口(已加载页面)后,当我尝试从浏览器获取任何内容(例如driver.page_source或任何find_element)时,出现了4s延迟.
I am opening multiple windows with the browser. Using firefox, my script is always fast. But on chrome, after switching to a window (with the page already loaded), when I try to get anything from the browser (like driver.page_source or any find_element), I get a 4s-ish delay.
我做错什么了吗?
[driver.execute_script('window.open(arguments[0]);', url) for url in urls]
for window in driver.window_handles[1:]:
driver.switch_to.window(window)
driver.page_source #it takes 4s here
driver.find_elements_by_class_name('class_name') #regular speed here (fast)
break
如果我用 driver.find_elements_by_class_name('class_name')
切换 driver.page_source
,第一个总是很慢.
If i switch driver.page_source
with driver.find_elements_by_class_name('class_name')
, the first one will be always slow.
推荐答案
正如您在问题中提到的, GeckoDriver / Firefox 组合要比 ChromeDriver 快em>/ Chrome 在这一点上,值得一提的是,不同的浏览器呈现了
As you mentioned in your question GeckoDriver / Firefox combination is faster then ChromeDriver / Chrome at this point it is worth to mention that diferent browsers render the HTML DOM in a different way.
您可以在 Chrome&Windows和Linux(硒)上的Firefox
此外,通过 Chrome Headless 会议,围绕不可预测的 CPU 和内存消耗进行了大量讨论.
Moreover, there had been a lot of discussion going around about the unpredictable CPU and Memory Consumption by Chrome Headless sessions.
根据讨论情况为...建立无头最低CPU +内存使用量 CPU +内存使用量可以通过以下方式优化:
As per the discussion Building headless for minimum cpu+mem usage the CPU + Memory usage can be optimized by:
- 使用自定义代理或C ++ ProtocolHandlers,您可以返回存根1x1像素的图像,甚至完全阻止它们.
- Chromium小组正在努力添加程序控制何时制作框架.目前,无头chrome仍在尝试以 60 fps 进行渲染,这非常浪费.许多页面的确需要几帧(也许 10-20 fps )才能正确呈现(由于使用了
requestAnimationFrame
和animation触发器
),但是我们期望这里可以节省很多CPU. - MemoryInfra 应该可以帮助您确定哪个组件是设置中最大的内存消耗者.
-
用法可以是:
- Using either a custom proxy or C++ ProtocolHandlers you could return stub 1x1 pixel images or even block them entirely.
- Chromium Team is working on adding a programmatic control over when frames are produced. Currently headless chrome is still trying to render at 60 fps which is rather wasteful. Many pages do need a few frames (maybe 10-20 fps) to render properly (due to usage of
requestAnimationFrame
andanimation triggers
) but we expect there are a lot of CPU savings to be had here. - MemoryInfra should help you determine which component is the biggest consumer of memory in your setup.
An usage can be:
$ headless_shell --remote-debugging-port=9222 --trace-startup=*,disabled-by-default-memory-infra http://www.chromium.org
铬总是将使用尽可能多的资源.如果要有效地限制其利用率,则应考虑使用您可以在限制chrome无头CPU和内存使用率中找到详细的讨论
这篇关于在通过Selenium和Python进行首页查询时,ChromeDriver的速度比GeckoDriver的速度慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!