【问题标题】:Chrome driver not working headlessly with Python and Selenium?Chrome 驱动程序不能与 Python 和 Selenium 一起工作?
【发布时间】:2018-08-06 10:35:15
【问题描述】:

我是使用 Python 进行 Selenium 编程的新手。该代码在没有 --headless 参数的情况下可以正常工作,但是当我尝试无头运行它时根本不会执行。有人可以帮我解决吗?

以下是我的示例代码,我使用的是 Python 3。

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

options = Options()
options.set_headless(headless=True)
driver = webdriver.Chrome(chrome_options=options, executable_path=r'E:/Python/[FreeTutorials.Us] Udemy - python-master-web-scraping-course-doing-20-real-projects/03 Step  _ Download HTML Content/chromedriver.exe')
driver.get("https://www.tirerack.com/survey/ValidationServlet?autoYear=2006&autoMake=Porsche&autoModel=911%20Carrera%20S%20Cabriolet&newDesktop=true")
print ("Headless Chrome Initialized")

html_doc=driver.page_source
soup= BeautifulSoup(html_doc,'lxml')

print(soup)
driver.quit()

【问题讨论】:

  • 路径中似乎有错误。更改 Python 主路径 (C:\Python34) 中的驱动程序并更改脚本中的路径。
  • 没有正确的路径,我已经检查过

标签: python-3.x web web-scraping


【解决方案1】:

我添加了 headless 作为争论而不是选项,它对我有用。试一试。

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

chrome_options = Options()
chrome_options.add_argument("--headless")
driver = webdriver.Chrome(chrome_options=chrome_options, executable_path='path to your chromedriver')
driver.get('https://stackoverflow.com/')

注意:始终将您的 chromedriver 保存在 python 的主路径中,即C:\Python34

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-21
    • 1970-01-01
    • 2013-07-11
    • 1970-01-01
    • 1970-01-01
    • 2016-01-01
    相关资源
    最近更新 更多