【发布时间】:2019-06-04 23:12:17
【问题描述】:
我正在尝试使用 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 网络驱动程序将按预期工作,直到您调用某个函数,然后它会抛出错误。
【问题讨论】:
标签: python selenium google-chrome selenium-webdriver selenium-chromedriver