如何改写代码适配最新Selenium版本(DeprecationWarning: executable_path)
解决Selenium 4中executable_path弃用及定位器错误问题
一、适配executable_path弃用问题
Selenium 4.0+移除了executable_path参数,需通过Service对象管理Chrome驱动:
- 导入ChromeService类:
from selenium.webdriver.chrome.service import Service
- 实例化Service对象并指定驱动路径:
service = Service('chromedriver.exe')
- 初始化浏览器时传入service参数:
browser = webdriver.Chrome(options=options, service=service)
二、修复定位器无效错误
错误中的invalid locator是因为Selenium 4要求明确指定定位策略,需配合By类使用:
- 导入By类:
from selenium.webdriver.common.by import By
- 修正用户名/密码输入框定位:
# 原错误写法 # input_username = browser.find_element("username") # input_password = browser.find_element("password") # 修正后 input_username = browser.find_element(By.ID, "username") input_password = browser.find_element(By.ID, "password")
- 替换已弃用的
find_element_by_xpath写法:
# 原错误写法 # browser.find_element_by_xpath('/html/body/div[1]/section/main/div/header/section').find_element_by_xpath("//*[text()='Message']") # 修正后 browser.find_element(By.XPATH, '/html/body/div[1]/section/main/div/header/section').find_element(By.XPATH, "//*[text()='Message']")
三、完整修改后的代码
from selenium import webdriver from selenium.webdriver.common.keys import Keys from selenium.webdriver.chrome.service import Service from selenium.webdriver.common.by import By import time, random from threading import Thread # 目标关注账号 target_profile = 'target' # 账号间操作间隔 delay = 10 def auth(browser, username, password): try: browser.get('https://instagram.com') time.sleep(random.randrange(2,4)) input_username = browser.find_element(By.ID, "username") input_password = browser.find_element(By.ID, "password") input_username.send_keys(username) time.sleep(random.randrange(1,2)) input_password.send_keys(password) time.sleep(random.randrange(1,2)) input_password.send_keys(Keys.ENTER) except Exception as err: print(err) browser.quit() def follow(username, password, target): options = webdriver.ChromeOptions() options.add_experimental_option("excludeSwitches", ["enable-logging"]) # 适配Selenium 4的驱动初始化方式 service = Service('chromedriver.exe') browser = webdriver.Chrome(options=options, service=service) browser.set_window_size(350, 900) auth(browser, username, password) try: time.sleep(random.randrange(8,10)) browser.get('https://instagram.com/' + target) time.sleep(random.randrange(3,6)) try: browser.find_element(By.XPATH, '/html/body/div[1]/section/main/div/header/section').find_element(By.XPATH, "//*[text()='Message']") print(f'[{username}] 已关注 "{target}"') time.sleep(random.randrange(2,4)) browser.quit() except: browser.find_element(By.XPATH, '/html/body/div[1]/section/main/div/header/section').find_element(By.XPATH, "//*[text()='Follow']").click() time.sleep(random.randrange(3,6)) browser.get('https://instagram.com/' + target) time.sleep(random.randrange(3,6)) try: browser.find_element(By.XPATH, '/html/body/div[1]/section/main/div/header/section').find_element(By.XPATH, "//*[text()='Message']") print(f'[{username}] 成功关注 "{target}"') time.sleep(random.randrange(2,4)) browser.quit() except: print(f'[{username}] 关注 "{target}" 失败') time.sleep(random.randrange(2,4)) browser.quit() except Exception as err: print(err) browser.quit() f = open('bots.txt', 'r') for line in f: log = line.split(':') follow(log[0], log[1].strip(), target_profile) # 处理换行符避免密码带多余空格 time.sleep(delay)
额外说明
- 新增
strip()处理账号密码行的换行符,避免密码包含多余空白字符 - 所有定位操作均已适配Selenium 4规范,同时解决了弃用警告和定位器错误问题
内容的提问来源于stack exchange,提问作者Pashka
相关产品推荐
相关产品推荐

