如何解决Python中WebDriver.__init__()意外参数'executable_path'错误
解决WebDriver.init()意外参数'executable_path'错误
问题原因
你遇到的错误是因为Selenium 4.0及以上版本已移除executable_path参数,原有驱动初始化方式不再适用,需改用Service类管理浏览器驱动。同时原代码中生日字段的操作存在逻辑问题(列表无法直接调用send_keys),一并修复。
修复步骤
- 导入
ChromeService类用于驱动管理 - 用
ChromeService指定驱动路径,传入webdriver.Chrome()完成初始化 - 调整生日下拉框的操作逻辑,逐个处理列表中的元素
修改后的完整代码
from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.common.exceptions import TimeoutException from selenium.webdriver.chrome.service import ChromeService # 新增导入 import random import string import time def generate_random_email(): return ''.join(random.choices(string.ascii_lowercase, k=10)) + '@example.com' def generate_random_username(): return 'user_' + ''.join(random.choices(string.ascii_lowercase + string.digits, k=8)) def generate_random_password(): return ''.join(random.choices(string.ascii_letters + string.digits + '!@#$%^&*', k=12)) def generate_birth_date(): year = random.randint(1980, 2000) month = random.randint(1, 12) day = random.randint(1, 28) return (year, month, day) chrome_options = webdriver.ChromeOptions() chrome_options.add_argument("--disable-notifications") chrome_options.add_argument("user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36") # 改用Service类初始化驱动 service = ChromeService(executable_path='chromedriver') driver = webdriver.Chrome(service=service, options=chrome_options) wait = WebDriverWait(driver, 15) driver.get('https://www.instagram.com/accounts/emailsignup/') try: cookie_button = wait.until(EC.element_to_be_clickable((By.XPATH, "//button[text()='Allow essential and optional cookies']"))) cookie_button.click() except TimeoutException: print("Cookie acceptance button not found. Proceeding without it.") email = generate_random_email() email_field = wait.until(EC.presence_of_element_located((By.NAME, 'emailOrPhone'))) email_field.send_keys(email) full_name = ' '.join(random.choices(string.ascii_letters, k=2)).title() name_field = driver.find_element(By.NAME, 'fullName') name_field.send_keys(full_name) username = generate_random_username() username_field = driver.find_element(By.NAME, 'username') username_field.send_keys(username) password = generate_random_password() password_field = driver.find_element(By.NAME, 'password') password_field.send_keys(password) year, month, day = generate_birth_date() birthday_fields = wait.until(EC.presence_of_all_elements_located((By.XPATH, "//select[@title='Year:'] | //select[@title='Month:'] | //select[@title='Day:']"))) # 按顺序处理年份、月份、日期下拉框 birthday_fields[0].send_keys(str(year)) birthday_fields[1].send_keys(str(month)) birthday_fields[2].send_keys(str(day)) time.sleep(2) submit_button = driver.find_element(By.XPATH, "//button[text()='Sign Up']") submit_button.click() try: error_element = wait.until(EC.visibility_of_element_located((By.ID, 'ssfErrorAlert'))) print("Error during signup:", error_element.text) except TimeoutException: print("Form submitted. Check for verification steps (if any).") input("Press Enter to close the browser...") driver.quit()
额外优化建议
可以使用webdriver-manager库自动管理ChromeDriver,无需手动下载和指定路径:
- 安装库:
pip install webdriver-manager - 修改驱动初始化代码:
from webdriver_manager.chrome import ChromeDriverManager from selenium.webdriver.chrome.service import ChromeService service = ChromeService(ChromeDriverManager().install()) driver = webdriver.Chrome(service=service, options=chrome_options)
内容的提问来源于stack exchange,提问作者Xvoque
相关产品推荐
相关产品推荐

