我正在尝试使用某些版本的chromium Web Driver的硒。为此,我在Debian上安装了webdriver_manager
(Windows 10 Pro上安装了WSL 2)
pip install webdriver_manager
我使用了selenium 4的README
包文件中推荐的行。我已经安装了4.1.5版本
# selenium 4
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from webdriver_manager.utils import ChromeType
driver = webdriver.Chrome(service=Service(ChromeDriverManager(chrome_type=ChromeType.CHROMIUM).install()))
但是我得到这个错误。我读到可能是selenium版本与这个特定版本的chromedriver不兼容,但是在selenium的快速参考指南中,下载版本是101.0.4951.41版本。我已经为Debian安装了最新的chrome版本和chromedriver,现在的版本是101.0.4951.64。由于所取的webdriver来自/home/db/.wdm/drivers/chromedriver/linux64/101.0.4951.41/chromedriver
,而不是系统中安装的,我不知道它是否必须与系统中安装的chrome版本匹配。它们必须完全匹配吗?
这是我运行python脚本后的错误:
====== WebDriver manager ======
Current chromium version is 101.0.4951
Get LATEST chromedriver version for 101.0.4951 chromium
Driver [/home/db/.wdm/drivers/chromedriver/linux64/101.0.4951.41/chromedriver] found in cache
Traceback (most recent call last):
File "/path/to/python/project/webdriver.py", line 21, in <module>
driver = webdriver.Chrome(
File "/path/to/python/project/env/lib/python3.9/site-packages/selenium/webdriver/chrome/webdriver.py", line 70, in __init__
super(WebDriver, self).__init__(DesiredCapabilities.CHROME['browserName'], "goog",
File "/path/to/python/project/env/lib/python3.9/site-packages/selenium/webdriver/chromium/webdriver.py", line 92, in __init__
RemoteWebDriver.__init__(
File "/path/to/python/project/env/lib/python3.9/site-packages/selenium/webdriver/remote/webdriver.py", line 275, in __init__
self.start_session(capabilities, browser_profile)
File "/path/to/python/project/env/lib/python3.9/site-packages/selenium/webdriver/remote/webdriver.py", line 365, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
File "/path/to/python/project/env/lib/python3.9/site-packages/selenium/webdriver/remote/webdriver.py", line 430, in execute
self.error_handler.check_response(response)
File "/path/to/python/project/env/lib/python3.9/site-packages/selenium/webdriver/remote/errorhandler.py", line 247, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: chrome not reachable
同时,我得到了另一个错误:
selenium.common.exceptions.WebDriverException: Message: unknown error: DevToolsActivePort file doesn't exist
我必须添加这段代码来修复它。但我不认为这将成为一个问题:
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("--remote-debugging-port=9222")
driver = webdriver.Chrome(
service=Service(
ChromeDriverManager(chrome_type=ChromeType.CHROMIUM).install(),
# executable_path='/usr/bin/chromium'
),
options=chrome_options
)
所以我不知道这是一个版本相关的问题还是WSL问题。我还能查些什么?
最后,解决我的问题是将标志--no-sandbox
和--headless
添加到选项对象
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("--no-sandbox") # without this error > "chrome not found"
chrome_options.add_argument("--headless") # without this error > "chrome not found"
chrome_options.add_argument("--remote-debugging-port=9222")
chrome_options.add_argument("--disable-dev-shm-using")
尽管如此,它仍然给出一些随机行为。我认为这是由于我在WSL系统内的网速很慢。我做了一些研究,但还是没有找到任何解决办法。我将发布另一个问题,看看是否有人知道一些解决方法。