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

时间:2018-05-30 14:13:15

标签: python selenium selenium-chromedriver headless chrome-canary

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")

2 个答案:

答案 0 :(得分:0)

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)

答案 1 :(得分:0)

如果您使用 Chrome Canary 作为基本Requirement,服务器仍然希望您根据底层操作系统架构在默认位置安装 Chrome 如下:

Chrome_binary_expected_location

您还可以按照以下文档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")