python - 无法使用 Selenium for Python 打开 Chrome WebDriver

标签 python google-chrome selenium ubuntu webdriver

我正在尝试以下脚本,但无法打开 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)

最佳答案

在使用 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()

Note : At the end of your Test Execution instead of close() invoke the quit() method so the WebDriver and WebBrowser both are destroyed.

关于python - 无法使用 Selenium for Python 打开 Chrome WebDriver,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49322339/

相关文章:

python - 在 matplotlib 中用 "X"而不是 "e"科学记数法显示数字

javascript - 检测 iFrame 中的打印事件

javascript - 在 Chrome DevTools 中禁用源映射

java - 如何从 JMeter 中的 header 获取值

python 读取.csv 文件 - 访问字段中的列表元素

python - 过滤掉pandas数据框中的重复数据

java - Selenium WebElement getCssValue 背景颜色被歪曲?

python - 使用 selenium ide 和 python 在 xpath 进行两次循环迭代后,我的程序失败

python - 按相似关系过滤图像列表

javascript - 在js文件中包含jquery