代码是:
import os
from selenium import webdriver
chromedriver = "/usr/bin/chromedriver"
os.environ["webdriver.chrome.driver"] = chromedriver
driver = webdriver.Chrome(chromedriver)
driver.get("http://stackoverflow.com")
driver.quit()
运行python name.py
返回消息
File "test_chr.py", line 6, in <module>
driver = webdriver.Chrome(chromedriver)
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/chrome/webdriver.py", line 69, in __init__
desired_capabilities=desired_capabilities)
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 92, in __init__
self.start_session(desired_capabilities, browser_profile)
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 179, in start_session
response = self.execute(Command.NEW_SESSION, capabilities)
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 236, in execute
self.error_handler.check_response(response)
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/errorhandler.py", line 192, 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.27.440175 (9bc1d90b8bfa4dd181fbbf769a5eb5e575574320),platform=Linux 4.4.0-64-generic x86_64)
我的硒版本 成功安装了selenium-3.0.2
ubuntu @ ip-172-31-30-227:/ usr / bin $ chromedriver -version ChromeDriver 2.27.440175(9bc1d90b8bfa4dd181fbbf769a5eb5e575574320)
google-chrome -version
Google Chrome 56.0.2924.87
我不知道如何解决问题
答案 0 :(得分:2)
您需要传递--headless
作为参数才能启动Chrome。您可以通过创建ChromeOption
对象并将其传递给ChromeDriver
。
>>> from selenium import webdriver
>>> from selenium.webdriver.common.action_chains import ActionChains
>>> from selenium.webdriver.chrome.options import Options
>>> chrome_options = Options()
>>> chrome_options.add_argument("--headless")
>>> driver = webdriver.Chrome(chrome_options=chrome_options)
>>> driver.get("https://www.google.com")
>>> driver.find_element_by_css_selector("img").get_attribute("alt")
'Google'