【问题标题】:Unable to Open Chrome WebDriver with Selenium for Python无法使用 Selenium for Python 打开 Chrome WebDriver
【发布时间】:2018-08-25 14:45:37
【问题描述】:

我正在尝试以下脚本,但无法打开 Webdriver:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

driver = webdriver.Chrome('/usr/bin/google-chrome')
driver.get("http://www.python.org")
assert "Python" in driver.title
elem = driver.find_element_by_name("q")
elem.clear()
elem.send_keys("pycon")
elem.send_keys(Keys.RETURN)
assert "No results found." not in driver.page_source
driver.close()

这会产生以下错误消息:

Traceback (most recent call last):
  File "scraper.py", line 4, in <module>
    driver = webdriver.Chrome('/usr/bin/google-chrome')
  File "/home/joseph/.local/lib/python2.7/site-packages/selenium/webdriver/chrome/webdriver.py", line 68, in __init__
    self.service.start()
  File "/home/joseph/.local/lib/python2.7/site-packages/selenium/webdriver/common/service.py", line 98, in start
    self.assert_process_still_running()
  File "/home/joseph/.local/lib/python2.7/site-packages/selenium/webdriver/common/service.py", line 111, in assert_process_still_running
    % (self.path, return_code)
selenium.common.exceptions.WebDriverException: Message: Service /usr/bin/google-chrome unexpectedly exited. Status code was: 1

我使用的是在 Windows 10 上运行的 Ubuntu 16.04。有什么想法吗?

编辑:

现在我正在使用 chromedriver 执行此操作,我将其解压缩到与脚本相同的目录。

driver = webdriver.Chrome(executable_path='./chromedriver')

我收到以下错误而不是前一个错误:

Traceback (most recent call last):
  File "scraper.py", line 4, in <module>
    driver = webdriver.Chrome(executable_path='./chromedriver')
  File "/home/joseph/.local/lib/python2.7/site-packages/selenium/webdriver/chrome/webdriver.py", line 75, in __init__
    desired_capabilities=desired_capabilities)
  File "/home/joseph/.local/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 154, in __init__
    self.start_session(desired_capabilities, browser_profile)
  File "/home/joseph/.local/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 243, in start_session
    response = self.execute(Command.NEW_SESSION, parameters)
  File "/home/joseph/.local/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 312, in execute
    self.error_handler.check_response(response)
  File "/home/joseph/.local/lib/python2.7/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: exited abnormally
  (Driver info: chromedriver=2.36.540471 (9c759b81a907e70363c6312294d30b6ccccc2752),platform=Linux 4.4.0-43-Microsoft x86_64)

【问题讨论】:

  • 您是否使用 Chrome 驱动程序运行它? (它是 WebDriver 用来控制 Chrome 的单独可执行文件)(Download Page)
  • @Aristu 我现在是,但它仍然不起作用。请查看更新后的帖子。
  • 您的 chrome 浏览器使用的 chromedriver 版本不兼容。
  • 确保 chromedriver.exe 文件在你的根 python 目录中

标签: python google-chrome selenium ubuntu webdriver


【解决方案1】:

在使用 Selenium v​​3.11.0ChromeDriver v2.36Chrome v64.x 时,您必须下载最新的 来自ChromeDriver - WebDriver for Chrome 的 ChromeDriver 并将其放置在您的系统中。接下来在初始化 WebDriverWebBrowser 时,您必须提及 ChromeDriver 的绝对路径,如下所示:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

driver = webdriver.Chrome(executable_path='/path/to/chromedriver')
driver.get("http://www.python.org")
assert "Python" in driver.title
elem = driver.find_element_by_name("q")
elem.clear()
elem.send_keys("pycon")
elem.send_keys(Keys.RETURN)
assert "No results found." not in driver.page_source
driver.quit()

注意:在您的 Test Execution 结束时,而不是 close() 调用 quit() 方法,以便 WebDriverWebBrowser 都被销毁了。

【讨论】:

  • 我将脚本更改为使用 chromedriver,但它仍然无法正常工作。请查看更新后的帖子。
  • 可以用绝对路径试试吗?
【解决方案2】:

您可能需要根据 chrome 的版本尝试不同版本的驱动程序。另外,请确保您的路径正确并指向可执行驱动程序文件(.exe) driver = webdriver.Chrome(executable_path='./chromedriver/chromedriver.exe')

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-08-12
    • 1970-01-01
    • 1970-01-01
    • 2018-01-04
    • 1970-01-01
    • 2014-10-22
    • 1970-01-01
    • 2014-11-04
    相关资源
    最近更新 更多