今天突然,我的项目中无法启动Selenium。错误信息如下:
main.py:11: DeprecationWarning: executable_path has been deprecated, please pass in a Service object
driver = webdriver.Chrome(options=options,executable_path='drivers/chromedriver-linux64/chromedriver')
Traceback (most recent call last):
File "/usr/local/lib/python3.10/runpy.py", line 196, in _run_module_as_main
return _run_code(code, main_globals, None,
File "/usr/local/lib/python3.10/runpy.py", line 86, in _run_code
exec(code, run_globals)
File "/root/.vscode-server/extensions/ms-python.python-2023.20.0/pythonFiles/lib/python/debugpy/adapter/../../debugpy/launcher/../../debugpy/__main__.py", line 39, in <module>
cli.main()
File "/root/.vscode-server/extensions/ms-python.python-2023.20.0/pythonFiles/lib/python/debugpy/adapter/../../debugpy/launcher/../../debugpy/../debugpy/server/cli.py", line 430, in main
run()
File "/root/.vscode-server/extensions/ms-python.python-2023.20.0/pythonFiles/lib/python/debugpy/adapter/../../debugpy/launcher/../../debugpy/../debugpy/server/cli.py", line 284, in run_file
runpy.run_path(target, run_name="__main__")
File "/root/.vscode-server/extensions/ms-python.python-2023.20.0/pythonFiles/lib/python/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_runpy.py", line 321, in run_path
return _run_module_code(code, init_globals, run_name,
File "/root/.vscode-server/extensions/ms-python.python-2023.20.0/pythonFiles/lib/python/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_runpy.py", line 135, in _run_module_code
_run_code(code, mod_globals, init_globals,
File "/root/.vscode-server/extensions/ms-python.python-2023.20.0/pythonFiles/lib/python/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_runpy.py", line 124, in _run_code
exec(code, run_globals)
File "/app/main.py", line 11, in <module>
driver = webdriver.Chrome(options=options,executable_path='drivers/chromedriver-linux64/chromedriver')
File "/usr/local/lib/python3.10/site-packages/selenium/webdriver/chrome/webdriver.py", line 69, in __init__
super().__init__(DesiredCapabilities.CHROME['browserName'], "goog",
File "/usr/local/lib/python3.10/site-packages/selenium/webdriver/chromium/webdriver.py", line 89, in __init__
self.service.start()
File "/usr/local/lib/python3.10/site-packages/selenium/webdriver/common/service.py", line 98, in start
self.assert_process_still_running()
File "/usr/local/lib/python3.10/site-packages/selenium/webdriver/common/service.py", line 110, in assert_process_still_running
raise WebDriverException(
selenium.common.exceptions.WebDriverException: Message: Service drivers/chromedriver-linux64/chromedriver unexpectedly exited. Status code was: 255
我的Dockerfile设置如下:
FROM python:3.10-buster
# Install necessary packages
RUN apt-get update && apt-get install -y \
curl unzip gettext python-babel \
ffmpeg \
poppler-utils \
fonts-takao-* fonts-wqy-microhei fonts-unfonts-core
# Install Chrome
RUN wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb && \
dpkg -i google-chrome-stable_current_amd64.deb; apt-get -fy install \
&& rm google-chrome-stable_current_amd64.deb
# Download and extract the latest version of ChromeDriver
RUN CHROME_DRIVER_VERSION=$(curl -sL "https://chromedriver.storage.googleapis.com/LATEST_RELEASE") && \
curl -sL "https://chromedriver.storage.googleapis.com/$CHROME_DRIVER_VERSION/chromedriver_linux64.zip" > chromedriver.zip && \
unzip chromedriver.zip -d /usr/local/bin && \
rm chromedriver.zip
# Install Python dependencies
COPY requirements.txt requirements.txt
RUN python -m pip install --upgrade pip && pip install -r requirements.txt
# Set and move to APP_HOME
ENV APP_HOME /app
WORKDIR $APP_HOME
ENV PYTHONPATH $APP_HOME
# Copy local code to the container image
COPY . .
需求.txt
selenium==4.15.1
而且,我创建了一个简单的 main.py
来启动 Selenium。
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = Options()
options.headless = True
options.add_argument('--no-sandbox')
options.add_argument('--disable-dev-shm-usage')
driver = webdriver.Chrome(options=options)
driver.get('https://www.google.com')
print(driver.title)
driver.quit()
我做了什么
我从此链接下载了119和120版本的驱动程序,将它们设置在executable_path
中,并执行它,但返回了相同的错误。
https://googlechromelabs.github.io/chrome-for-testing/#stable
请帮助我!
环境
MacOS 13.6 苹果M2 Docker桌面版4.21.1
最佳答案
在 Selenium 4 中,executable_path
已弃用,您必须将 Service()
类的实例与 ChromeDriverManager().install()
结合使用
- Deprecate all but
Options
andService
arguments in driver instantiation. (#9125,#9128)
https://github.com/SeleniumHQ/selenium/blob/d6acda7c0254f9681574bf4078ff2001705bf940/py/CHANGES#L101
你可以尝试:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))
driver.get("https://www.your_website_.com")
编辑:在查看您的示例后,我认为您可以尝试:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
service = Service(r"C:\chromedriver.exe") # your path to chromedriver executable file
options = webdriver.ChromeOptions()
driver = webdriver.Chrome(service=service, options=options)
driver = webdriver.Chrome(options=options)
driver.get('https://www.google.com')
print(driver.title)
driver.quit()
关于python - 无法启动 Selenium,遇到 DeprecationWarning 和 WebDriverException 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/77416725/