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

Selenium无法点击Goalscorers关联div元素问题求助

解决方案
  • 方案1:添加显式等待,确保元素完全加载可交互后再执行点击
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

# 最长等待10秒,直到元素满足可点击条件
wait = WebDriverWait(driverBet, 10)
target = wait.until(EC.element_to_be_clickable((By.XPATH, "//div[@data-test-market='Goalscorers']")))
target.click()
  • 方案2:原生点击无效时,使用JS强制触发点击事件(可绕过大部分前端交互限制、元素遮挡问题)
target = driverBet.find_element(By.XPATH, "//div[@data-test-market='Goalscorers']")
driverBet.execute_script("arguments[0].click();", target)
  • 方案3:适配悬停才激活交互的场景,先模拟鼠标悬停再执行点击
from selenium.webdriver.common.action_chains import ActionChains

target = driverBet.find_element(By.XPATH, "//div[@data-test-market='Goalscorers']")
# 先悬停0.5秒激活元素交互状态,再执行点击
ActionChains(driverBet).move_to_element(target).pause(0.5).click().perform()
  • 方案4:更换定位目标,选择绑定点击事件的上层容器或交互按钮
    从你提供的DOM结构判断,点击事件大概率绑定在Goalscorers所在的整行容器,或右侧的下拉箭头图标上,可尝试定位这些元素执行操作:
# 定位整行容器点击
row_target = driverBet.find_element(By.XPATH, "//div[@data-test-market='Goalscorers']/ancestor::div[contains(@class,'_1b7dz8zNaN')]")
driverBet.execute_script("arguments[0].click();", row_target)

# 或定位右侧下拉箭头点击
arrow_target = driverBet.find_element(By.XPATH, "//div[@data-test-market='Goalscorers']/following-sibling::div[@class='_w81afw']//svg")
arrow_target.click()

补充说明:如果以上方案都无效,先检查目标元素是否嵌套在iframe内,如果是需要先调用driverBet.switch_to.frame(对应iframe元素)切换到iframe上下文后再执行操作。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.26 00:15:01