使用Headless Chrome Webdriver运行Selenium [英] Running Selenium with Headless Chrome Webdriver

查看:182
本文介绍了使用Headless Chrome Webdriver运行Selenium的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我正在尝试硒的一些东西,我真的希望它能很快.

So I'm trying some stuff out with selenium and I really want it to be quick.

所以我的想法是,使用无头chrome运行它可以使我的脚本更快.

So my thought is that running it with headless chrome would make my script faster.

首先,这种假设是正确的,还是如果我使用无头驱动程序运行脚本也没关系吗?

First is that assumption correct, or does it not matter if i run my script with a headless driver?

无论如何,我仍然希望它能够正常运行,但是我却以某种方式不能尝试,我尝试了不同的方法,并且大多数人建议它可以按照十月更新中的说明进行工作

Anyways I still want to get it to work to run headless, but I somehow can't, I tried different things and most suggested that it would work as said here in the October update

如何将ChromeDriver配置为通过Selenium以无头模式启动Chrome浏览器?

但是当我尝试这样做时,我得到了奇怪的控制台输出,但它似乎仍然无法正常工作.

But when I try that, I get weird console output and it still doesn't seem to work.

感谢任何小费.

推荐答案

要运行chrome-headless,只需通过chrome_options.add_argument添加--headless,即:

To run chrome-headless just add --headless via chrome_options.add_argument, i.e.:

from selenium import webdriver from selenium.webdriver.chrome.options import Options
chrome_options = Options()
#chrome_options.add_argument("--disable-extensions")
#chrome_options.add_argument("--disable-gpu")
#chrome_options.add_argument("--no-sandbox") # linux only
chrome_options.add_argument("--headless")
# chrome_options.headless = True # also works
driver = webdriver.Chrome(options=chrome_options)
start_url = "https://duckgo.com"
driver.get(start_url)
print(driver.page_source.encode("utf-8"))
driver.quit()
# b'<!DOCTYPE html><html xmlns="http://www....


所以我的想法是,使用无头镀铬来运行它会使我 脚本更快.

So my thought is that running it with headless chrome would make my script faster.

尝试使用诸如--disable-extensions--disable-gpu之类的Chrome选项并对其进行基准测试,但我认为不会有太大的改进.

Try using chrome options like --disable-extensions or --disable-gpu and benchmark it, but I wouldn't count with much improvement.

参考文献: headless-chrome

注意:截至今天,在Windows上运行chrome headless时,您添加--disable-gpu标志 参见 crbug.com/737678

Note: As of today, when running chrome headless on Windows., you should include the  --disable-gpu flag See crbug.com/737678

这篇关于使用Headless Chrome Webdriver运行Selenium的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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