简体   繁体   English

无法使用 Selenium for Python 打开 Chrome WebDriver

[英]Unable to Open Chrome WebDriver with Selenium for Python

I am trying the following script and can't get it to open 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()

This produces the following error message:这会产生以下错误消息:

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

I'm using Ubuntu 16.04 running on Windows 10. Any ideas what this could be?我正在使用在 Windows 10 上运行的 Ubuntu 16.04。你知道这可能是什么吗?

EDIT:编辑:

Now I'm doing this with chromedriver, which I unzipped to the same directory as the script.现在我正在使用 chromedriver 执行此操作,我将其解压缩到与脚本相同的目录中。

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

I get the following error instead of the previous one:我收到以下错误而不是前一个错误:

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)

While working with Selenium v3.11.0 , ChromeDriver v2.36 and Chrome v64.x you have to download the latest ChromeDriver from the ChromeDriver - WebDriver for Chrome and place it within your system.在使用Selenium v​​3.11.0ChromeDriver v2.36Chrome v64.x 时,您必须从ChromeDriver - WebDriver for Chrome下载最新的ChromeDriver并将其放置在您的系统中。 Next while initializing the WebDriver and the WebBrowser you have to mention the absolute path of the ChromeDriver as follows :接下来在初始化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.注意:在测试执行结束时调用quit()方法而不是close() quit()方法,因此WebDriverWebBrowser都被销毁。

You may need to try various versions of the driver depending on version of chrome.您可能需要根据 chrome 的版本尝试各种版本的驱动程序。 Also, make sure your path is correct and pointing to executable driver file(.exe) driver = webdriver.Chrome(executable_path='./chromedriver/chromedriver.exe')另外,请确保您的路径正确并指向可执行驱动程序文件(.exe) driver = webdriver.Chrome(executable_path='./chromedriver/chromedriver.exe')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM