Chrome 使用Selenium找不到Chromium二进制文件

7qhs6swi  于 2023-08-01  发布在  Go
关注(0)|答案(2)|浏览(306)

我在MacOS服务器上使用Selenium。我已经安装了chrome和chromedriver,但是在我的代码中,我得到一个错误,说找不到二进制文件。
这就是代码:

options = webdriver.ChromeOptions()
if(env['HEADLESS']):
    options.add_argument('--headless')
options.add_argument('--auto-show-cursor')
options.add_argument('--no-sandbox')
options.add_argument('--disable-dev-shm-usage')
try:
    driver = webdriver.Chrome('chromedriver',options=options)
except WebDriverException:
    # Exception occurred, try using the specified executable_path
    chrome_binary_path = '/usr/bin/chromedriver'
    options.binary_location = chrome_binary_path
    driver = webdriver.Chrome('chromedriver', options=options)# I have previously tried driver = webdriver.Chrome(executable_path = chrome_binary_path, options=options)

字符串
这就是错误:

Traceback (most recent call last):
  File "/home/docker/web-testing-wtai/WTAI-project/src/BL/browsers.py", line 21, in chrome
    driver = webdriver.Chrome('chromedriver',options=options)
  File "/home/docker/web-testing-wtai/WTAI-project/env/lib/python3.10/site-packages/selenium/webdriver/chrome/webdriver.py", line 80, in __init__
    super().__init__(
  File "/home/docker/web-testing-wtai/WTAI-project/env/lib/python3.10/site-packages/selenium/webdriver/chromium/webdriver.py", line 104, in __init__
    super().__init__(
  File "/home/docker/web-testing-wtai/WTAI-project/env/lib/python3.10/site-packages/selenium/webdriver/remote/webdriver.py", line 286, in __init__
    self.start_session(capabilities, browser_profile)
  File "/home/docker/web-testing-wtai/WTAI-project/env/lib/python3.10/site-packages/selenium/webdriver/remote/webdriver.py", line 378, in start_session
    response = self.execute(Command.NEW_SESSION, parameters)
  File "/home/docker/web-testing-wtai/WTAI-project/env/lib/python3.10/site-packages/selenium/webdriver/remote/webdriver.py", line 440, in execute
    self.error_handler.check_response(response)
  File "/home/docker/web-testing-wtai/WTAI-project/env/lib/python3.10/site-packages/selenium/webdriver/remote/errorhandler.py", line 245, in check_response
    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 /snap/chromium/2529/usr/lib/chromium-browser/chrome is no longer running, so ChromeDriver is assuming that Chrome has crashed.)
Stacktrace:
#0 0x560c936c76a3 <unknown>
#1 0x560c9340cad6 <unknown>
#2 0x560c934354c6 <unknown>
#3 0x560c934318a0 <unknown>
#4 0x560c9346f78d <unknown>
#5 0x560c9346ef6f <unknown>
#6 0x560c93466993 <unknown>
#7 0x560c9343c414 <unknown>
#8 0x560c9343d47e <unknown>
#9 0x560c9368aacd <unknown>
#10 0x560c9368f505 <unknown>
#11 0x560c93698a0e <unknown>
#12 0x560c9368ff8c <unknown>
#13 0x560c93660a62 <unknown>
#14 0x560c936b1538 <unknown>
#15 0x560c936b16dc <unknown>
#16 0x560c936c0b35 <unknown>
#17 0x7f8232ae6b43 <unknown>

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/docker/web-testing-wtai/WTAI-project/src/BL/utils.py", line 418, in initializeBrowser
    driver = browsers.chrome(env)
  File "/home/docker/web-testing-wtai/WTAI-project/src/BL/browsers.py", line 26, in chrome
    driver = webdriver.Chrome('chromedriver', options=options)
  File "/home/docker/web-testing-wtai/WTAI-project/env/lib/python3.10/site-packages/selenium/webdriver/chrome/webdriver.py", line 80, in __init__
    super().__init__(
  File "/home/docker/web-testing-wtai/WTAI-project/env/lib/python3.10/site-packages/selenium/webdriver/chromium/webdriver.py", line 104, in __init__
    super().__init__(
  File "/home/docker/web-testing-wtai/WTAI-project/env/lib/python3.10/site-packages/selenium/webdriver/remote/webdriver.py", line 286, in __init__
    self.start_session(capabilities, browser_profile)
  File "/home/docker/web-testing-wtai/WTAI-project/env/lib/python3.10/site-packages/selenium/webdriver/remote/webdriver.py", line 378, in start_session
    response = self.execute(Command.NEW_SESSION, parameters)
  File "/home/docker/web-testing-wtai/WTAI-project/env/lib/python3.10/site-packages/selenium/webdriver/remote/webdriver.py", line 440, in execute
    self.error_handler.check_response(response)
  File "/home/docker/web-testing-wtai/WTAI-project/env/lib/python3.10/site-packages/selenium/webdriver/remote/errorhandler.py", line 245, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: no chrome binary at /usr/bin/chromedriver


我通过执行nano /usr/bin/chromedriver确保文件位于该路径,并得到以下结果:

#!/bin/sh
if ! [ -x /snap/bin/chromium.chromedriver ]; then
    echo "" >&2
    echo "Command '$0' requires the chromium snap to be installed." >&2
    echo "Please install it with:" >&2
    echo "" >&2
    echo "snap install chromium" >&2
    echo "" >&2
    exit 1
fi
exec /snap/bin/chromium.chromedriver "$@"


我真的不明白selenium为什么找不到chromedriver可执行文件。我做错了什么?或者,如何避免指定路径,就像我在得到异常之前所做的那样?当我在一台新的计算机上使用我的脚本时,我开始得到这个异常,该计算机具有相同的 selenium 版本(4.8.2),但更高的 chrome 版本(在以前的计算机中,我有114.0.5735.90,现在是114.0.5735.198)。如果chromedriver版本不匹配是问题所在,我如何仅使用命令行更改它?

e37o9pze

e37o9pze1#

您可以使用webdriver_manager直接下载并启动驱动程序,而无需提供路径。
您可以使用webdriver_manager安装pip3 install webdriver_manager

from webdriver_manager.chrome import ChromeDriverManager
from selenium import webdriver

driver = webdriver.Chrome(executable_path=ChromeDriverManager().install())

driver.get("Your URL")

字符串

mtb9vblg

mtb9vblg2#

根据您的错误no chrome binary at /usr/bin/chromedriver,您可能将二进制位置(Chrome所在的位置)与chromedriver位置混淆。这些是不一样的。
由于selenium4.10.0最近的更改,您对webdriver.Chrome()的调用将不再有效:https://github.com/SeleniumHQ/selenium/commit/9f5801c82fb3be3d5850707c46c3f8176e3ccd8e


的数据
(Note第一个参数不再是executable_path,而是options
如果你想传入一个executable_path,你现在必须使用service参数:

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

service = Service(executable_path='/usr/bin/chromedriver')
options = webdriver.ChromeOptions()
options.add_argument('--headless')
options.add_argument('--auto-show-cursor')
options.add_argument('--no-sandbox')
options.add_argument('--disable-dev-shm-usage')
driver = webdriver.Chrome(service=service, options=options)
# ...
driver.quit()

字符串
(如果在PATH上找不到chromedriver,Selenium现在会为您下载它。)

相关问题