为什么在带有 Chromium 的 python selenium 中无头不起作用?

问题描述

我正在尝试在我的 RaspBerry Pi 上无头运行代码。在正常模式下它工作得很好,但如果我试图让它无头,代码“忽略”它。 我尝试了不同的无头方式,有 - 或没有,它没有改变任何东西。 我当前的代码看起来是这样:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
import time

options = Options()
options.add_argument ("-headless")
options.add_argument ("-disable-gpu")
allesFertig = True


driver = webdriver.Chrome(options =  options,executable_path ='/usr/lib/chromium-browser/chromedriver')
driver = webdriver.Chrome()

有什么解决办法吗?

解决方法

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
import time

options = Options()
options.add_argument ("headless")
options.add_argument ("disable-gpu")
allesFertig = True


driver = webdriver.Chrome(options =  options,executable_path ='/usr/lib/chromium-browser/chromedriver')

只需删除第二个驱动程序,您将再次创建 chrome 实例而没有选项,这就是它打开 GUI 的原因