无法使用Selenium for Python打开Chrome WebDriver

时间:2018-03-16 13:49:15

标签: 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)

2 个答案:

答案 0 :(得分:0)

使用 Selenium v​​3.11.0 ChromeDriver v2.36 Chrome v64.x 时,您必须下载最新的来自enter image description here的ChromeDriver 并将其放置在您的系统中。接下来,在初始化 WebDriver WebBrowser 时,您必须提及 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()
  

注意:在测试执行的末尾而不是close()调用quit()方法,以便 WebDriver WebBrowser 都被销毁。

答案 1 :(得分:0)

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