python - 为什么chrome webdriver配置无效?

标签 python selenium google-chrome selenium-chromedriver

我正在尝试使用 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 code>,我的 ChromeDriver 是 2.46

所以它们都是最新的,而且看起来都很兼容。

有什么问题吗?

最佳答案

我在 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 以 headless 模式启动,因为我假设您的 Ubuntu 上没有运行桌面。 --no-sandbox 标志会关闭 Linux 中的沙箱,这有时会在开始时导致问题。

关于python - 为什么chrome webdriver配置无效?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54866203/

相关文章:

css - selenium firefox css 选择器崩溃

google-chrome - 如何使用 uglifyjs2 和 Ionic 加载 sourcemaps?

javascript - 通过 Chrome 扩展程序静默下载

python - 基于 Python 中较小的数据集生成较大的综合数据集

python - Python 中的 Hive 子字符串等效项

python - 如何使用 selenium TouchActions python 点击​​特定元素内部的坐标?

java - 1 次测试运行后无法读取文件,org.apache.poi.EmptyFileException : The supplied file was empty (zero bytes long)

python-3.x - 如何使用 Selenium 在youtube中获得所有评论?

python - 如果既不是按位运算也不是 print chevron,>> 是做什么的?

python - 动态加载两个 libpython 版本