我正在尝试使用Selenium驱动程序运行刮板
我的py文件是(刚刚开始)
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
# Set the url we want
<and so on>
但是我遇到以下错误:
Traceback (most recent call last):
File "main.py", line 37, in <module>
driver = webdriver.Chrome(chrome_options=options)
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/chrome/webdriver.py", line 81, in __init__
desired_capabilities=desired_capabilities)
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 157, in __init__
self.start_session(capabilities, browser_profile)
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 252, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_respons
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: exited abnormally
(unknown error: DevToolsActivePort file doesn't exist)
(The process started from chrome location /usr/bin/google-chrome is no longer running, so ChromeDriver is assuming has crashed.)
(Driver info: chromedriver=2.46.628388 (4a34a70827ac54148e092aafb70504c4ea7ae926),platform=Linux 4.4.0-140-generic
搜索此错误,似乎它在ChromeDriver和Google-Chrome和Selenium中不兼容,但是我的Selenium版本是3.141.0
,我的Chrome版本是72.0.3626.119
,而我的ChromeDriver是{{1} }。
所以它们都是最新的,而且看起来都兼容。
任何想法有什么问题吗?
答案 0 :(得分:0)
我在Ubuntu 18.04上遇到了此错误,这可能是导致Chrome浏览器崩溃的几件事之一。尝试设置这些标志:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = Options()
options.add_argument("--no-sandbox")
options.add_argument("--headless")
driver = webdriver.Chrome("/usr/local/bin/chromedriver", chrome_options=options)
--headless
标志可确保Chrome以无头模式启动,因为我假设您的Ubuntu上没有运行桌面。 --no-sandbox
标志会关闭Linux中的沙箱,这有时会在一开始就引起问题。