无法使用Selenium在无头模式下运行Chromedriver

lsmepo6l  于 2023-03-06  发布在  Go
关注(0)|答案(1)|浏览(211)

运行我的selenium脚本时,我遇到了一个错误,我不知道如何传递。我已经在谷歌上搜索并尝试了各种解决方案,但它们都不起作用。以下是我的代码:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

chrome_driver_binary = '/home/user/Projects/myproject/chromedriver'
options = Options()
options.add_argument("--start-maximized")
options.add_argument("--no-sandbox") 
options.add_argument("--disable-dev-shm-usage")
options.add_argument("--headless")
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option('useAutomationExtension', False)
driver = webdriver.Chrome(executable_path=chrome_driver_binary, options = options)
driver.get('http://www.ubuntu.com/')

每次我运行这个我得到的错误“进程开始从 chrome 位置/usr/bin/google-chrome不再运行,所以ChromeDriver是假设 chrome 已崩溃。”
然而,如果我注解掉“options.add_argument(“--headless”)“这一行,我的代码就能正常工作,但是我不能让它注解掉,因为我需要脚本在headless模式下运行。
我在VirtualBox 7上使用Ubuntu 22.04,Python 3.10.6,并在虚拟环境中运行我的代码。

zlhcx6iw

zlhcx6iw1#

您可以使用更新的无头模式(适用于Chrome 109及更新版本):https://stackoverflow.com/a/73840130/7058266
options.add_argument("--headless=new")
如果你使用的是旧版本的Chrome浏览器,语法会有所不同(详情请查看链接)。

相关问题