今天突然,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 . .
型
requirements.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
1条答案
按热度按时间0vvn1miw1#
在Selenium 4中,
executable_path
被弃用,您必须使用ChromeDriverManager().install()
的Service()
类的示例Options
和Service
之外的所有参数。(#9125,#9128)https://github.com/SeleniumHQ/selenium/blob/d6acda7c0254f9681574bf4078ff2001705bf940/py/CHANGES#L101
您可以尝试:
字符串
编辑:看了你的例子后,我想你可以试试:
型