【问题标题】:How to initiate Chrome Canary in headless mode through Selenium and Python如何通过 Selenium 和 Python 以无头模式启动 Chrome Canary
【发布时间】:2018-05-30 14:13:15
【问题描述】:
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浏览器仍然会拉起
  • 您使用的 chrome 和 chrome 驱动程序版本是什么?如果您的 chrome 版本 > 59,请尝试使用 options.add_argument('--headless')
  • 我的版本是 67.0

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


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:
    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)
    

    【讨论】:

    • 虽然这个答案可能是正确且有用的,但最好在其中附上一些解释来解释它如何帮助解决问题。如果有更改(可能不相关)导致它停止工作并且用户需要了解它曾经是如何工作的,这在未来变得特别有用。
    • 另外,我在windows上
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-30
    相关资源
    最近更新 更多