Traceback (most recent call last): File "D:\python实验\ruance\auto.py", line 323, in <module> driver = webdriver.Chrome(executable_path=chrome_driver_path, options=options) # 加载驱动并应用配置[^3] ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ TypeError: WebDriver.__init__() got an unexpected keyword argument 'executable_path'请修改你的代码的报错
时间: 2025-06-05 12:34:20 浏览: 40
### Selenium 4及以上版本中正确初始化Chrome WebDriver的方法
在 Selenium 4.10.0 及更高版本中,`executable_path` 参数已被弃用,取而代之的是 `Service` 类来指定 ChromeDriver 的路径[^3]。以下是适配最新版本 Selenium WebDriver 的完整代码示例。
```python
from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
### 配置 ChromeDriver 路径
chrome_driver_path = r"D:\python实验\ruance\chromedriver-win64\chromedriver.exe" # 指定驱动路径
# 使用 Service 初始化 Chrome WebDriver
service = ChromeService(executable_path=chrome_driver_path) # 使用 Service 指定路径[^3]
options = webdriver.ChromeOptions()
options.add_argument('--disable-gpu') # 禁用 GPU 加速,避免部分系统报错
options.add_argument('--no-sandbox') # 解决 DevToolsActivePort 文件不存在的报错
options.add_argument('--headless') # 无头模式运行浏览器(可选)
driver = webdriver.Chrome(service=service, options=options) # 初始化 WebDriver
try:
driver.get("https://www.12306.cn/index/") # 打开 12306 官网
# 等待页面加载完成
WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.ID, "fromStationText"))
)
# 输入出发地:南京
from_station_input = driver.find_element(By.ID, "fromStationText")
from_station_input.click()
from_station_input.send_keys("南京")
from_station_input.send_keys(Keys.ENTER)
# 输入目的地:上海
to_station_input = driver.find_element(By.ID, "toStationText")
to_station_input.click()
to_station_input.send_keys("上海")
to_station_input.send_keys(Keys.ENTER)
# 设置出发日期为 2023-05-27
date_picker = driver.find_element(By.ID, "train_date")
date_picker.clear()
date_picker.send_keys("2023-05-27") # 替换为实际日期
date_picker.send_keys(Keys.ENTER)
# 提交查询
search_button = driver.find_element(By.ID, "search_one")
search_button.click()
# 等待查询结果加载
WebDriverWait(driver, 20).until(
EC.presence_of_element_located((By.CLASS_NAME, "ticket-info"))
)
# 筛选符合条件的列车
train_list = driver.find_elements(By.XPATH, "//tbody[@id='queryLeftTable']/tr")
filtered_trains = []
for train in train_list:
try:
train_number = train.find_element(By.CLASS_NAME, "number").text.strip()
start_time = train.find_element(By.CLASS_NAME, "cdz").text.strip()
end_time = train.find_element(By.CLASS_NAME, "ddz").text.strip()
duration = train.find_element(By.CLASS_NAME, "ls").text.strip()
# 筛选条件:南京南站到上海虹桥站,12:00-18:00 的智能动车组
if (train_number.startswith("D") or train_number.startswith("G")) and \
"南京南" in train.text and "上海虹桥" in train.text and \
"12:00" <= start_time <= "18:00":
filtered_trains.append({
"车次": train_number,
"出发时间": start_time,
"到达时间": end_time,
"历时": duration
})
except Exception as e:
continue
# 输出筛选结果
if filtered_trains:
print(f"符合条件的列车数量: {len(filtered_trains)}")
shortest_train = min(filtered_trains, key=lambda x: x["历时"]) # 找出历时最短的车次
print(f"历时最短车次: {shortest_train['车次']}, 出发时间: {shortest_train['出发时间']}, 到达时间: {shortest_train['到达时间']}")
else:
print("未找到符合条件的列车")
finally:
# 关闭浏览器
time.sleep(5) # 延迟关闭,方便观察结果
driver.quit()
```
---
#### 说明
1. **更新初始化方式**:在 Selenium 4.10.0 及更高版本中,通过 `Service` 类指定 ChromeDriver 的路径,替代了旧版的 `executable_path` 参数。
2. **兼容性与性能优化**:添加了 `--headless`、`--disable-gpu` 和 `--no-sandbox` 参数以优化性能并避免潜在的兼容性问题[^3]。
3. **异常处理**:在遍历列车列表时增加了异常捕获,确保即使某些元素无法定位也不会中断整个脚本运行。
---
阅读全文
相关推荐


















