我的 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)
设置浏览器二进制路径或驱动程序路径不是强制性的,您可以让selenium以编程方式为您下载匹配的驱动程序。
参考这些:
您可以简化代码并尝试以下操作:
from selenium import webdriver
url = "https://www.python.org"
driver = webdriver.Chrome()
driver.get(url)