Python Selenium:“ selenium.common.exceptions.WebDriverException:消息:无法访问Chrome”本地文件

时间:2019-06-04 23:12:17

标签: python selenium google-chrome selenium-webdriver selenium-chromedriver

我正在尝试使用Selenium来解析名为output.html的本地HTML文件。

在Python解释器中,我可以进行导入,创建webdriver.Chrome驱动程序对象和GET我的本地文件。

当尝试使用驱动程序的功能查找任何内容时,我会收到错误消息。

代码:

>>> from selenium import webdriver
>>> from selenium.webdriver.chrome.options import Options
>>> 
>>> chrome_options = Options()
>>> chrome_options.binary_location = '/usr/bin/google-chrome'
>>> chrome_options.add_argument('--headless')
>>> chrome_options.add_argument('--no-sandbox')
>>> chrome_options.add_argument('--disable-dev-shm-usage')
>>> 
>>> driver = webdriver.Chrome(chrome_options=chrome_options)
>>> 
>>> driver.get('file:output.html')
>>> 
>>> # no error up to here
>>> 
>>> driver.name  # runs ok
>>> driver.orientation  # runs ok
>>>
>>> driver.page_source  # error!
>>> driver.find_element_by_name('p_system')  # error!

我对错误的原因感到困惑。我在Google上找到的每个页面都表明chromedriver和/或Google Chrome二进制文件放置在错误的位置,或者Selenium找不到该位置,但事实并非如此,因为我可以成功使用GET与驱动程序(与本地HTML文件一起使用),并且可以在https://www.python.org之类的网站上运行相同的代码。

错误回溯:

selenium.common.exceptions.WebDriverException: Message: chrome not reachable
  (Session info: headless chrome=74.0.3729.169)
  (Driver info: chromedriver=74.0.3729.6 (255758eccf3d244491b8a1317aa76e1ce10d57e9-refs/branch-heads/3729@{#29}),platform=Linux 4.4.0-17763-Microsoft x86_64

重复项:

虽然很容易将问题标记为重复并继续进行,但最好复查问题,至少检查它们之间是否存在差异。

其他Stack Overflow问题之间的主要区别在于,该问题适用于外部网站,但不适用于本地文件。其他版本根本不起作用,更改版本可以解决此问题。

如错误回溯中所示,chromedriver版本和无头chrome版本均为74,并且应与此site兼容。

Selenium网络驱动程序将按预期工作,直到您调用某个函数,然后它将引发错误。

1 个答案:

答案 0 :(得分:0)

尝试使用完整的文件路径,如下面的示例所示。

url = r"file:///C:/Users/xxxx/Desktop/delte.html"
driver.get(url)
相关问题