Suddenly today, Selenium could not be launched in my project. The error messages are as follows:
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
The settings of my Dockerfile are as follows:
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
And, I created a simple main.py
to just launch 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()
I downloaded the drivers of version 119 and 120 from this link, set them in executable_path
, and executed it, but the same error was returned.
https://googlechromelabs.github.io/chrome-for-testing/#stable
Please help me!
MacOS 13.6 Apple M2 Docker desktop 4.21.1
In Selenium 4 executable_path
is deprecated you have to use an instance of the Service()
class with ChromeDriverManager().install()
- Deprecate all but
Options
andService
arguments in driver instantiation. (#9125,#9128)
https://github.com/SeleniumHQ/selenium/blob/d6acda7c0254f9681574bf4078ff2001705bf940/py/CHANGES#L101
You can try:
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")
Edit: After looking at your example I think you can try:
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()