1 环境:python3.9 pycharm 2020+ selenium 121+chrome121
chrome version:
Chrome 版本 121.0.6167.140(正式版本) (64 位)
2 下载 && 安装:selenium :
下载网址:https://googlechromelabs.github.io/chrome-for-testing/,将文件解压到项目下的/driver 下
安装 :pychram 3.9 terminal 下venv 下:
pip3.11 install selenium
3 运行代码如下:
import time
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
service = Service("driver/chrome-headless-shell.exe")
driver = webdriver.Chrome(service=service)
driver.get('https://passport.bilibili.com/login')
报错如下:
C:\Users\Administrator\PycharmProjects\ve1\venv\Scripts\python.exe C:/Users/Administrator/PycharmProjects/ve1/ve1.py Traceback (most recent call last): File "C:\Users\Administrator\PycharmProjects\ve1\ve1.py", line 6, in <module> driver = webdriver.Chrome(service=service) File "C:\Users\Administrator\PycharmProjects\ve1\venv\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 45, in init super().init( File "C:\Users\Administrator\PycharmProjects\ve1\venv\lib\site-packages\selenium\webdriver\chromium\webdriver.py", line 49, in init self.service.path = DriverFinder.get_path(self.service, options) File "C:\Users\Administrator\PycharmProjects\ve1\venv\lib\site-packages\selenium\webdriver\common\driver_finder.py", line 44, in get_path raise NoSuchDriverException(f"Unable to locate or obtain driver for {options.capabilities['browserName']}") selenium.common.exceptions.NoSuchDriverException: Message: Unable to locate or obtain driver for chrome; For documentation on this error, please visit: https://www.selenium.dev/documentation/webdriver/troubleshooting/errors/driver_location
4 解决:
server 指定的路径 未找到相关文 件,将:service=service选项去掉,成功运行。