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

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

问题描述

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

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

首先,这个假设是否正确,或者我是否使用无头驱动程序运行我的脚本无关紧要?

无论如何,我仍然想让它运行无头运行,但我不知何故不能,我尝试了不同的方法,大多数人建议它可以像 10 月更新中所说的那样运行

如何配置 ChromeDriver通过 Selenium 在 Headless 模式下启动 Chrome 浏览器?

但是当我尝试这样做时,我得到了奇怪的控制台输出,但它似乎仍然不起作用.

感谢任何提示.

解决方案

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

from selenium import webdriver从 selenium.webdriver.chrome.options 导入选项chrome_options = 选项()#chrome_options.add_argument(--disable-extensions")#chrome_options.add_argument(--disable-gpu")#chrome_options.add_argument("--no-sandbox") # 仅适用于 linuxchrome_options.add_argument(--headless")# chrome_options.headless = True # 也可以驱动程序 = webdriver.Chrome(options=chrome_options)start_url = "https://duckgo.com";driver.get(start_url)打印(driver.page_source.encode(utf-8"))驱动程序退出()# b'<!DOCTYPE html><html xmlns="http://www....


<块引用>

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

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


参考:headless-chrome

<块引用>

注意:截至今天,在 Windows 上运行无头 chrome 时,您应该包括 --disable-gpu 标志请参阅 crbug.com/737678

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

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

How to configure ChromeDriver to initiate Chrome browser in Headless mode through Selenium?

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

Any tipps appreciated.

解决方案

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.

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


References: headless-chrome

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天全站免登陆