Selenium+Python爬取板球比分时按钮点击拦截问题求助
解决Selenium点击「Glenelg 1st Innings」按钮被拦截的问题
错误提示element click intercepted: Element is not clickable at point (316, -283)说明目标元素在当前视图的上方不可见区域,或是被其他元素遮挡了,并非页面屏蔽了按钮功能。试试下面几种解决方法:
1. 先滚动到元素可视区域再点击
把元素滚动到页面中间位置,确保它在可视范围内:
from selenium import webdriver from selenium.webdriver.common.by import By driver = webdriver.Chrome() targetURL = "https://www.playhq.com/cricket-australia/org/saca-premier-cricket/senior-men-summer-202324/west-end-mens-2nd-grade/game-centre/7cb22178" driver.get(targetURL) other_innings_button = driver.find_element(by=By.XPATH, value="//button[text()='Glenelg 1st Innings']") # 滚动到元素居中位置 driver.execute_script("arguments[0].scrollIntoView({block: 'center'});", other_innings_button) other_innings_button.click()
也可以用ActionChains实现滚动+点击:
from selenium.webdriver.common.action_chains import ActionChains ActionChains(driver).move_to_element(other_innings_button).click().perform()
2. 用显式等待确保元素可点击
隐式等待仅负责等待元素被找到,但不保证元素处于可点击状态,改用显式等待元素完全可交互:
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC wait = WebDriverWait(driver, 10) other_innings_button = wait.until(EC.element_to_be_clickable((By.XPATH, "//button[text()='Glenelg 1st Innings']"))) other_innings_button.click()
3. 直接用JavaScript触发点击
如果元素被弹窗、导航栏等其他元素遮挡,常规点击无法触发时,直接用JS调用元素的点击事件:
other_innings_button = driver.find_element(by=By.XPATH, value="//button[text()='Glenelg 1st Innings']") driver.execute_script("arguments[0].click();", other_innings_button)
内容的提问来源于stack exchange,提问作者Ian Symonds
相关产品推荐
相关产品推荐

