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

运行Python自动化脚本遇TypeError:WebDriver不支持executable_path参数

问题解决方案

错误原因

Selenium 4.x及以上版本移除了webdriver.Chrome()的executable_path参数,改用Service类管理驱动程序路径,传入该参数会触发类型错误。

修复步骤

  1. 导入Service类
    在脚本开头添加导入语句:
from selenium.webdriver.chrome.service import Service
  1. 修改驱动初始化代码
    将原代码中初始化Chrome驱动的行:
bot = webdriver.Chrome(options=options, executable_path=CM().install())

替换为:

service = Service(executable_path=CM().install())
bot = webdriver.Chrome(service=service, options=options)

额外优化(可选)

Selenium 4.x已弃用find_element_by_xpath、find_elements_by_xpath这类旧方法,建议改用新API,需先导入By类:

from selenium.webdriver.common.by import By

然后替换代码中的查找元素方法,例如:

  • bot.find_element_by_xpath(xpath) → bot.find_element(By.XPATH, xpath)
  • bot.find_elements_by_xpath(xpath) → bot.find_elements(By.XPATH, xpath)

修改后的完整脚本

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.common.exceptions import NoSuchElementException
import os
import time

HOW_MANY = int(input("How many comments you want to like (0-20):"))

while HOW_MANY > 20:
    print("Cant like more than 20 comments, please choose a smaller number!")
    HOW_MANY = int(input("How many comments you want to like (0-20):"))


options = webdriver.ChromeOptions()
options.add_argument("--log-level=3")
options.add_argument(f"--user-data-dir={os.getcwd()}\\profile")
mobile_emulation = {
    "userAgent": "Mozilla/5.0 (Linux; Android 4.2.1; en-us; Nexus 5 Build/JOP40D) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/90.0.1025.166 Mobile Safari/535.19"
}
options.add_experimental_option("mobileEmulation", mobile_emulation)
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(executable_path=CM().install())
bot = webdriver.Chrome(service=service, options=options)
bot.set_window_position(0, 0)
bot.set_window_size(414, 936)

url_file = open("urls.txt", "r")
urls = url_file.readlines()


def doesnt_exist(bot, xpath):
    try:
        bot.find_element(By.XPATH, xpath)
    except NoSuchElementException:
        return True
    else:
        return False


for url in urls:
    print("--Liking comments for this post: " + url)

    bot.get(url)

    if not doesnt_exist(bot, "/html/body/div[5]/div/div/div[3]/button[2]"):
        time.sleep(1)
        bot.find_element(By.XPATH, "/html/body/div[5]/div/div/div[3]/button[2]").click()
        print("Closed pop ups")
    else:
        print("No pop up window.")

    # pause
    time.sleep(4)
    bot.find_element(By.XPATH,
        '//*[@id="main"]/div/div[1]/div[1]/div/div[3]/div/div/div[1]/div'
    ).click()

    # click on comments
    time.sleep(1)
    bot.find_element(By.XPATH,
        '//*[@id="main"]/div/div[1]/div[1]/div/div[3]/div/div/div[2]/div/div[2]'
    ).click()
    time.sleep(2)

    try:
        l_buttons = bot.find_elements(By.XPATH,
            "/html/body/div[2]/div/div[2]/div[2]/div/div[1]/div[2]/div"
        )

        for l_button in l_buttons[:HOW_MANY]:
            l_button.click()
            time.sleep(1)

    except NoSuchElementException:
        print("Couldnt like, comments are disabled.")

print("FINISHED")
bot.quit()

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.12 09:44:53