简体   繁体   English

在通过Selenium和Python进行首页查询时,ChromeDriver的速度比GeckoDriver慢

[英]ChromeDriver is too slower than GeckoDriver on the first page query through Selenium and Python

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). 我有最新版本的驱动程序(chromedriver = 2.44.609551),硒包(3.141.0)和(无头chrome = 70.0.3538.110)。 (on windows) (在Windows上)

I am opening multiple windows with the browser. 我正在使用浏览器打开多个窗口。 Using firefox, my script is always fast. 使用firefox,我的脚本总是很快。 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. 但是在chrome上,切换到窗口(已加载页面)后,当我尝试从浏览器获取任何内容(例如driver.page_source或任何find_element)时,出现了4s延迟。

Am I doing something wrong? 难道我做错了什么?

[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

If i switch driver.page_source with driver.find_elements_by_class_name('class_name') , the first one will be always slow. 如果我用driver.find_elements_by_class_name('class_name')切换driver.page_source ,则第一个总是很慢。

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. 正如您在问题中提到的那样, 此时GeckoDriver / Firefox组合的速度比ChromeDriver / Chrome快,值得一提的是,不同的浏览器以不同的方式呈现HTML DOM

You can find a detailed discussion in Chrome & Firefox on Windows vs Linux (selenium) 您可以在Windows和Linux(硒)上的Chrome和Firefox中找到详细的讨论

Moreover, there had been a lot of discussion going around about the unpredictable CPU and Memory Consumption by Chrome Headless sessions. 此外,关于Chrome Headless会话无法预测的CPU内存消耗的讨论也很多。

As per the discussion Building headless for minimum cpu+mem usage the CPU + Memory usage can be optimized by: 根据讨论, 构建最低的cpu + mem使用率 ,可以通过以下方式优化CPU +内存使用率:

  • Using either a custom proxy or C++ ProtocolHandlers you could return stub 1x1 pixel images or even block them entirely. 使用自定义代理或C ++ ProtocolHandlers,您可以返回存根1x1像素图像,甚至完全阻止它们。
  • Chromium Team is working on adding a programmatic control over when frames are produced. Chromium小组正在努力增加何时制作框架的程序控制 Currently headless chrome is still trying to render at 60 fps which is rather wasteful. 目前,无头Chrome仍在尝试以60 fps进行渲染,这非常浪费。 Many pages do need a few frames (maybe 10-20 fps ) to render properly (due to usage of requestAnimationFrame and animation triggers ) but we expect there are a lot of CPU savings to be had here. 许多页面的确需要几帧(可能是10-20 fps )才能正确渲染(由于使用了requestAnimationFrameanimation triggers ),但我们希望在此节省大量CPU。
  • MemoryInfra should help you determine which component is the biggest consumer of memory in your setup. MemoryInfra应该帮助您确定哪个组件是设置中最大的内存使用者。
  • An usage can be: 用法可以是:

     $ headless_shell --remote-debugging-port=9222 --trace-startup=*,disabled-by-default-memory-infra http://www.chromium.org 
  • Chromium is always going to use as much resources as are available to it. 铬将始终使用尽可能多的资源。 If you want to effectively limit it's utilization, you should look into using cgroups 如果要有效限制其利用率,则应考虑使用cgroups

You can find a detailed discussion in Limit chrome headless CPU and memory usage 您可以在限制Chrome无头CPU和内存使用情况中找到详细的讨论

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM