如何通过 Selenium 和 Python 以无头模式启动 Chrome Canary

How to initiate Chrome Canary in headless mode through Selenium and Python

from selenium import webdriver

options = webdriver.ChromeOptions()
options.binary_location = 'C:\Users\mpmccurdy\Desktop\Google Chrome Canary.lnk'
options.add_argument('headless')
options.add_argument('window-size=1200x600')
driver = webdriver.Chrome(chrome_options=options)
driver.get("https://www.python.org")
chrome_options = Options()
chrome_options.add_argument("--headless")
path = os.getcwd() +'\chromedriver.exe' #needs to be in your current working directory
driver = webdriver.Chrome(chrome_options=chrome_options, executable_path=path)

如果您使用 Chrome Canary 作为基本 Requirement,服务器仍然希望您拥有 Chrome 根据底层 OS 体系结构安装在默认位置,如下所示:

您还可以按照文档 Using a Chrome executable in a non-standard location 覆盖默认 Chrome 二进制位置,如下所示:

from selenium import webdriver

options = webdriver.ChromeOptions()
options.binary_location = r'C:\Program Files (x86)\Google\Chrome\Application\chrome.exe'
options.add_argument('--headless')
options.add_argument('window-size=1200x600')
driver = webdriver.Chrome(executable_path=r'C:\path\to\chromedriver.exe', chrome_options=options)
driver.get("https://www.python.org")