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

Java Selenium+JUnit点击单选按钮遇ElementClickInterceptedException求助

解决Selenium点击单选按钮被广告拦截的问题

问题原因

你的代码已定位到单选按钮,但页面加载的谷歌广告iframe覆盖在按钮上方,导致实际点击被广告元素拦截,抛出ElementClickInterceptedException。

可行解决方案

方案1:使用JavaScript执行点击操作

直接通过JS绕过元素遮挡问题,强制触发点击事件:

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.edge.EdgeDriver;

public class Assign2 {
    WebDriver driver;
    JavascriptExecutor js;

    @BeforeEach
    public void setUp(){
        driver = new EdgeDriver();
        js = (JavascriptExecutor) driver;
        driver.manage().window().maximize();
        driver.navigate().to("https://demoqa.com/radio-button");
    }

    @Test
    public void radioChecker(){
        By radio1 = By.id("yesRadio");
        WebElement radioElement = driver.findElement(radio1);
        // 用JS执行点击
        js.executeScript("arguments[0].click();", radioElement);
        
        boolean isSelect1 = radioElement.isSelected();
        Assertions.assertTrue(isSelect1);
    }

    @AfterEach
    public void quitTap(){
        driver.quit();
    } 
}

方案2:等待元素可点击+滚动到元素位置

先等待元素处于可点击状态,同时将元素滚动到视图中,避免被广告遮挡:

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import java.time.Duration;

public class Assign2 {
    WebDriver driver;
    WebDriverWait wait;

    @BeforeEach
    public void setUp(){
        driver = new EdgeDriver();
        wait = new WebDriverWait(driver, Duration.ofSeconds(10));
        driver.manage().window().maximize();
        driver.navigate().to("https://demoqa.com/radio-button");
    }

    @Test
    public void radioChecker(){
        By radio1 = By.id("yesRadio");
        WebElement radioElement = wait.until(ExpectedConditions.elementToBeClickable(radio1));
        // 滚动到元素位置
        ((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", radioElement);
        radioElement.click();
        
        boolean isSelect1 = radioElement.isSelected();
        Assertions.assertTrue(isSelect1);
    }

    @AfterEach
    public void quitTap(){
        driver.quit();
    } 
}

方案3:隐藏广告iframe(可选)

如果广告iframe的ID固定,可以通过JS隐藏它:

// 在点击单选按钮前添加这段代码
WebElement adIframe = driver.findElement(By.id("google_ads_iframe_/21849154601,22343295815/Ad.Plus-Anchor_0"));
((JavascriptExecutor) driver).executeScript("arguments[0].style.display='none';", adIframe);

说明

  • 方案1是最直接的解决方式,适配大部分元素遮挡场景;
  • 方案2更符合Selenium交互最佳实践,先确保元素可交互再操作;
  • 方案3仅适用于广告iframe的ID固定不变的情况,若广告ID动态生成则不适用。

内容的提问来源于stack exchange,提问作者HADEEL هديل

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.28 15:07:03