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

如何解决Selenium中的ElementClickInterceptedException点击拦截错误?

解决Selenium的ElementClickInterceptedException问题

问题描述

ElementClickInterceptedException: element click intercepted: Element is not clickable at point (288, 833)

手动滚动页面让元素可见后,Selenium能正常找到并点击元素,但必须依赖手动辅助滚动。已尝试代码中注释的显式等待、Actions点击、JS点击等方法,问题仍未解决,求可行解决方案。

报错代码

package mypackage;

import java.time.Duration;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import io.github.bonigarcia.wdm.WebDriverManager;

public class DynamicWebTable {

    public static void main(String[] args) throws InterruptedException {
        WebDriverManager.chromedriver().setup();
        WebDriver driver = new ChromeDriver();
        
        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
        
        driver.get("https://demo.opencart.com/admin/");
        driver.manage().window().maximize();
        
        
        WebElement username = driver.findElement(By.id("input-username"));
        username.clear();
        username.sendKeys("demo");
        
        WebElement password = driver.findElement(By.id("input-password"));
        password.clear();
        password.sendKeys("demo");
        
        driver.findElement(By.xpath("//button[normalize-space()='Login']")).click();
        
        //Close popup
        if(driver.findElement(By.xpath("//div[@class='modal-content']")).isDisplayed()) {
            driver.findElement(By.xpath("//button[@class='btn-close']")).click();
        }
        
        driver.findElement(By.xpath("//a[normalize-space()='Sales']")).click();
        driver.findElement(By.xpath("//a[normalize-space()='Orders']")).click();
        
        //get total no of pages
        String textWithTotalPages = driver.findElement(By.xpath("//div[@class='col-sm-6 text-end']")).getText();
        int pages = getNumberOfPages(textWithTotalPages);
        System.out.println(pages);
        
        //
        for(int p = 1; p <= 2; p++) {
            WebElement active_page = driver.findElement(By.xpath("//ul[@class='pagination']//li//span"));
            System.out.println("Active Page: "+active_page.getText());
            Thread.sleep(3000);
            //new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.elementToBeClickable(active_page)).click();
            active_page.click();
            //Actions actions = new Actions(driver);
            //actions.moveToElement(active_page).click().perform();
            //JavascriptExecutor e = (JavascriptExecutor)driver;
            //e.executeScript("arguments[0].click();", active_page);
            
            //get number of rows
            int rows = driver.findElements(By.xpath("//table[@class='table table-bordered table-hover']//tbody/tr")).size();
            System.out.println("No of Rows: "+rows);
            
            //read rows from each page
            for(int r=1; r<=rows; r++) {
                String orderId = driver.findElement(By.xpath("//table[@class='table table-bordered table-hover']//tbody//tr["+r+"]//td[2]")).getText();
                String store = driver.findElement(By.xpath("//table[@class='table table-bordered table-hover']//tbody//tr["+r+"]//td[3]")).getText();
                String customer = driver.findElement(By.xpath("//table[@class='table table-bordered table-hover']//tbody//tr["+r+"]//td[4]")).getText();
                String status = driver.findElement(By.xpath("//table[@class='table table-bordered table-hover']//tbody//tr["+r+"]//td[5]")).getText();
                
                System.out.println(orderId+ "  "+store+"  "+customer+"  "+status);
                
            }
            
            //click next page
            String nextPage = Integer.toString(p + 1);
            driver.findElement(By.xpath("//ul[@class='pagination']//li//a[text()='"+nextPage+"']")).click(); 
            
                    
        }
        
        
        driver.quit();
    }
        
    //extract number of pages from String 
    public static int getNumberOfPages(String text){
        
        return Integer.valueOf(text.substring(text.indexOf("(")+1, text.indexOf("Pages")-1));
        
    }
    
}

解决方案

1. 强制滚动元素到视图中心

在点击元素前,用JavaScriptExecutor将元素滚动到浏览器视图中心,确保元素完全可见:

WebElement targetElement = driver.findElement(By.xpath("//ul[@class='pagination']//li[@class='active']//span"));
JavascriptExecutor js = (JavascriptExecutor) driver;
// 滚动元素到视图中心,避免部分可见导致的拦截
js.executeScript("arguments[0].scrollIntoView({block: 'center', inline: 'center'});", targetElement);
// 短时间等待元素稳定
Thread.sleep(500);
targetElement.click();

2. 检查并处理遮挡元素

页面底部的固定导航栏、弹窗等常导致元素被拦截,可提前检测并规避:

JavascriptExecutor js = (JavascriptExecutor) driver;
// 检查底部是否有固定遮挡元素,如有则向上滚动避开
try {
    WebElement footer = driver.findElement(By.tagName("footer"));
    if(footer.isDisplayed()){
        js.executeScript("window.scrollBy(0, -120);");
    }
} catch (NoSuchElementException e) {
    // 无底部元素则跳过
}

3. 优化显式等待逻辑

结合元素可见性、可点击性双重条件,确保元素状态稳定后再操作:

WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(15));
WebElement activePage = wait.until(ExpectedConditions.and(
    ExpectedConditions.visibilityOfElementLocated(By.xpath("//ul[@class='pagination']//li[@class='active']//span")),
    ExpectedConditions.elementToBeClickable(By.xpath("//ul[@class='pagination']//li[@class='active']//span"))
));
// 滚动后点击
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].scrollIntoView(true);", activePage);
activePage.click();

4. 修正分页元素定位

原代码中active_page的XPath未限定active类,可能定位到错误元素,调整定位规则:

// 精准定位当前激活的分页按钮
WebElement active_page = driver.findElement(By.xpath("//ul[@class='pagination']//li[@class='active']//span"));

完整修改后的核心循环代码

for(int p = 1; p <= 2; p++) {
    WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(15));
    // 准确定位激活的分页按钮
    WebElement active_page = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//ul[@class='pagination']//li[@class='active']//span")));
    System.out.println("Active Page: "+active_page.getText());
    
    // 滚动元素到视图中心
    JavascriptExecutor js = (JavascriptExecutor) driver;
    js.executeScript("arguments[0].scrollIntoView({block: 'center'});", active_page);
    // 等待元素可点击后点击
    wait.until(ExpectedConditions.elementToBeClickable(active_page)).click();
    
    // 获取当前页行数并读取数据
    int rows = driver.findElements(By.xpath("//table[@class='table table-bordered table-hover']//tbody/tr")).size();
    System.out.println("No of Rows: "+rows);
    
    for(int r=1; r<=rows; r++) {
        String orderId = driver.findElement(By.xpath("//table[@class='table table-bordered table-hover']//tbody//tr["+r+"]//td[2]")).getText();
        String store = driver.findElement(By.xpath("//table[@class='table table-bordered table-hover']//tbody//tr["+r+"]//td[3]")).getText();
        String customer = driver.findElement(By.xpath("//table[@class='table table-bordered table-hover']//tbody//tr["+r+"]//td[4]")).getText();
        String status = driver.findElement(By.xpath("//table[@class='table table-bordered table-hover']//tbody//tr["+r+"]//td[5]")).getText();
        
        System.out.println(orderId+ "  "+store+"  "+customer+"  "+status);
    }
    
    // 处理下一页点击,同样加入滚动和等待
    String nextPage = Integer.toString(p + 1);
    WebElement nextBtn = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//ul[@class='pagination']//li//a[text()='"+nextPage+"']")));
    js.executeScript("arguments[0].scrollIntoView({block: 'center'});", nextBtn);
    wait.until(ExpectedConditions.elementToBeClickable(nextBtn)).click();
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.03 14:40:20