Python Selenium登录IMVU后无法点击span元素问题如何解决
修复方案
核心原因是IMVU登录态下开启了前端点击事件拦截,原生Selenium点击会被拦截,同时硬等待无法保证元素完全处于可交互状态,修改后代码如下:
from selenium import webdriver from selenium.webdriver.chrome.options import Options import time from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC # 路径前加r避免转义问题 PATH = r"C:\chromedriver\chromedriver.exe" options = Options() options.headless = False options.add_argument("--mute-audio") # 加参数避免网站检测到自动化工具 options.add_argument("--disable-blink-features=AutomationControlled") options.add_experimental_option("excludeSwitches", ["enable-automation"]) options.add_experimental_option("useAutomationExtension", False) driver = webdriver.Chrome(PATH, options=options) # 移除webdriver标识 driver.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", { "source": "Object.defineProperty(navigator, 'webdriver', {get: () => undefined})" }) def login(): driver.get("https://secure.imvu.com/welcome/login/") # 显式等待元素可点击再操作 WebDriverWait(driver, 10).until(EC.element_to_be_clickable( (By.XPATH, '/html/body/section/div/div/div/section/div/div/button') )).click() WebDriverWait(driver, 10).until(EC.element_to_be_clickable( (By.XPATH, '//*[@id="__default__"]/div[1]/div[2]/div/div[2]/div/ul/a') )).click() username = WebDriverWait(driver, 10).until(EC.visibility_of_element_located( (By.XPATH, '/html/body/div[6]/section[2]/div/div/div/section/form/div[1]/input') )) password = WebDriverWait(driver, 10).until(EC.visibility_of_element_located( (By.XPATH, '/html/body/div[6]/section[2]/div/div/div/section/form/div[2]/div[2]/input') )) username.send_keys("你的账号") password.send_keys("你的密码") WebDriverWait(driver, 10).until(EC.element_to_be_clickable( (By.XPATH, '/html/body/div[6]/section[2]/div/div/div/section/form/div[4]/button') )).click() # 等待登录完成 time.sleep(5) def claim(): driver.get("https://avatars.imvu.com/qub") # 等待目标元素加载完成 target_span = WebDriverWait(driver, 10).until(EC.visibility_of_element_located( (By.XPATH, "//span[contains(@id,'grant-badge-74354585-1')]") )) # 滚动到元素位置 driver.execute_script("arguments[0].scrollIntoView({block: 'center'});", target_span) time.sleep(1) # 用JS点击绕开前端拦截 driver.execute_script("arguments[0].click();", target_span) time.sleep(5) login() claim()
- 额外优化点:添加了反自动化检测参数,避免网站识别到Selenium工具限制操作,可大幅提升操作成功率。
- 如果需要适配其他徽章,只需修改
claim函数里的XPATH规则和目标页面地址即可。
内容的提问来源于stack exchange,提问作者iWYO
相关产品推荐
相关产品推荐

