You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在Eclipse中使用Selenium Firefox时遇TypeError:executable_path参数无效

解决Selenium初始化Firefox时的TypeError问题

问题原因

Selenium 4.x版本起,webdriver.Firefox()不再支持executable_path参数,该参数已被移除,驱动路径需要通过Service类来指定管理。

解决步骤

  1. 你已经导入了Service类,直接用它封装GeckoDriver路径
  2. 创建Service实例并传入驱动路径,再将service参数传递给webdriver.Firefox()
  3. 移除无意义的driver = webdriver.Firefox()空初始化代码

修改后的核心代码片段

# 浏览器配置
options = Options()
options.add_argument('--start-maximized')
options.add_argument('--disable-extensions')

driver_path = r'C:\\x\\x\\x\\x\\geckodriver-v0.33.0-win32\\geckodriver.exe'

# 初始化Service实例
service = Service(executable_path=driver_path)
# 通过service参数启动Firefox Driver
driver = webdriver.Firefox(service=service, options=options)

额外优化提示

代码中使用的driver.find_element_by_xpath()属于Selenium旧版API,已被弃用,建议替换为新版写法:

# 旧写法(已弃用)
texto_columnas = driver.find_element_by_xpath('/html/body/div[7]/main/div[4]/div/section[4]/section/div[1]/ul')

# 新写法
texto_columnas = driver.find_element(By.XPATH, '/html/body/div[7]/main/div[4]/div/section[4]/section/div[1]/ul')

完整修改后的代码

from selenium import webdriver
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.firefox.service import Service
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import time
import pandas as pd

# 浏览器配置
options = Options()
options.add_argument('--start-maximized')
options.add_argument('--disable-extensions')

driver_path = r'C:\\x\\x\\x\\x\\geckodriver-v0.33.0-win32\\geckodriver.exe'

# 初始化Service和Driver
service = Service(executable_path=driver_path)
driver = webdriver.Firefox(service=service, options=options)

# 窗口定位到第二屏幕
driver.set_window_position(2000, 0)
driver.maximize_window()
time.sleep(1)

# 打开目标网站
driver.get('https://eltiempo.es')

# 关闭隐私提示
WebDriverWait(driver, 5)\
    .until(EC.element_to_be_clickable((By.CSS_SELECTOR,
                                      'button.didomi-components-button didomi-button didomi-dismiss-button didomi-components-button--color didomi-button-highlight highlight-button'.replace(' ', '.'))))\
    .click()

# 搜索马德里
WebDriverWait(driver, 5)\
    .until(EC.element_to_be_clickable((By.CSS_SELECTOR,
                                      'input#inputSearch')))\
    .send_keys('Madrid')

WebDriverWait(driver, 5)\
    .until(EC.element_to_be_clickable((By.CSS_SELECTOR,
                                      'i.icon.icon-search')))\
    .click()

# 进入天气详情页
WebDriverWait(driver, 5)\
    .until(EC.element_to_be_clickable((By.CSS_SELECTOR,
                                      'i.icon_weather_s.icon.icon-local')))\
    .click()

WebDriverWait(driver, 5)\
    .until(EC.element_to_be_clickable((By.XPATH,
                                      '/html/body/div[7]/main/div[4]/div/section[4]/section/div/article/section/ul/li[2]/a')))\
    .click()

# 获取天气数据
WebDriverWait(driver, 5)\
    .until(EC.element_to_be_clickable((By.XPATH,
                                      '/html/body/div[7]/main/div[4]/div/section[4]/section/div[1]/ul')))

texto_columnas = driver.find_element(By.XPATH, '/html/body/div[7]/main/div[4]/div/section[4]/section/div[1]/ul')
texto_columnas = texto_columnas.text

tiempo_hoy = texto_columnas.split('Mañana')[0].split('\n')[1:-1]

horas = list()
temp = list()
v_viento = list()

for i in range(0, len(tiempo_hoy), 4):
    horas.append(tiempo_hoy[i])
    temp.append(tiempo_hoy[i+1])
    v_viento.append(tiempo_hoy[i+2])

# 保存数据到CSV
df = pd.DataFrame({'Horas': horas, 'Temperatura': temp, 'V_viento(km_h)':v_viento})
print(df)
df.to_csv('tiempo_hoy.csv', index=False)

driver.quit()

内容的提问来源于stack exchange,提问作者Mila Data

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.10 05:05:11