pycharm Chrome驱动程序无法启动,可能是由于路径错误(Selenium、Python)

8fsztsew  于 2023-10-20  发布在  PyCharm
关注(0)|答案(2)|浏览(339)

我的Selenium程序无法启动。我很久以前就这样做了,由于软件更新,我的旧程序也无法运行。我怀疑我给了Chrome和驱动程序错误的路径,因为驱动程序“启动失败”,但我不确定。我在PyCharm中运行Python,机器是Linux(因此没有exe文件)。

from selenium import webdriver
from selenium.webdriver.chrome.service import Service

url = "https://www.python.org"
options = webdriver.ChromeOptions()
options.binary_location = "/home/username/Documents/.../my-bot/chrome-linux64/chrome"
chrome_driver_binary = "/home/username/Documents/.../my-bot/chromedriver_linux64/chromedriver"
driver = webdriver.Chrome(service=Service(chrome_driver_binary), options=options)
driver.get(url)

我得到的错误消息:

"/home/username/Documents/.../my-bot/venv/bin/python" /home/username/Documents/.../my-bot/main.py 
Traceback (most recent call last):
  File "/home/username/Documents/.../my-bot/main.py", line 12, in <module>
    driver = webdriver.Chrome(service=Service(chrome_driver_binary), options=options)
  File "/home/username/Documents/.../my-bot/venv/lib/python3.10/site-packages/selenium/webdriver/chrome/webdriver.py", line 45, in __init__
    super().__init__(
  File "/home/username/Documents/.../my-bot/venv/lib/python3.10/site-packages/selenium/webdriver/chromium/webdriver.py", line 56, in __init__
    super().__init__(
  File "/home/username/Documents/.../my-bot/venv/lib/python3.10/site-packages/selenium/webdriver/remote/webdriver.py", line 205, in __init__
    self.start_session(capabilities)
  File "/home/username/Documents/.../my-bot/venv/lib/python3.10/site-packages/selenium/webdriver/remote/webdriver.py", line 289, in start_session
    response = self.execute(Command.NEW_SESSION, caps)["value"]
  File "/home/username/Documents/.../my-bot/venv/lib/python3.10/site-packages/selenium/webdriver/remote/webdriver.py", line 344, in execute
    self.error_handler.check_response(response)
  File "/home/username/Documents/.../my-bot/venv/lib/python3.10/site-packages/selenium/webdriver/remote/errorhandler.py", line 229, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: crashed.
  (unknown error: DevToolsActivePort file doesn't exist)
  (The process started from chrome location /home/username/Documents/.../my-bot/chrome-linux64/chrome is no longer running, so ChromeDriver is assuming that Chrome has crashed.)
Stacktrace:
#0 0x55db688604e3 <unknown>
#1 0x55db6858fc76 <unknown>
#2 0x55db685b8d78 <unknown>
#3 0x55db685b5029 <unknown>
#4 0x55db685f3ccc <unknown>
#5 0x55db685f347f <unknown>
#6 0x55db685eade3 <unknown>
#7 0x55db685c02dd <unknown>
#8 0x55db685c134e <unknown>
#9 0x55db688203e4 <unknown>
#10 0x55db688243d7 <unknown>
#11 0x55db6882eb20 <unknown>
#12 0x55db68825023 <unknown>
#13 0x55db687f31aa <unknown>
#14 0x55db688496b8 <unknown>
#15 0x55db68849847 <unknown>
#16 0x55db68859243 <unknown>
#17 0x7f9e5c28f25a <unknown>
#18 0x7f9e5c317fc4 clone

Process finished with exit code 1

我试着把它作为一个服务来运行,它也不起作用。我试着运行Firefox,它没有改变任何东西。
几个月前我试着运行我自己的程序,它也不工作(如果我没记错的话,gecko驱动程序在PATH变量中,但Selenium可能由于更新而不再工作)。这是旧程序的一个片段:

from selenium import webdriver
import requests
from selenium.webdriver.firefox.options import Options as FirefoxOptions
import traceback

url = "https://www.python.org"
options = webdriver.FirefoxOptions()
driver = webdriver.Firefox(options=options)
driver.get(url)
bxgwgixi

bxgwgixi1#

DevToolsActivePort file doesn't exist的两个主要原因是:

  1. Linux操作系统
    1.尝试在无头环境中运行有头浏览器(又是Linux)
    第一个问题可以通过更改Chrome选项来解决:
options.add_argument("--disable-dev-shm-usage")
options.add_argument("--no-sandbox")

第二个问题可以通过在无头模式下运行来解决:

options.add_argument("--headless")  # Old headless Chrome
# OR:
options.add_argument("--headless=new")  # New headless Chrome
aiazj4mn

aiazj4mn2#

设置浏览器二进制路径或驱动程序路径不是强制性的,您可以让selenium以编程方式为您下载匹配的驱动程序。
参考这些:

你可以简化你的代码,并尝试下面的方法:

from selenium import webdriver

url = "https://www.python.org"
driver = webdriver.Chrome()
driver.get(url)

相关问题