我在使用 Java Selenium 和 JUnit 单击单选按钮时遇到问题。
虽然语法显示正确且按钮已找到,但我仍然遇到异常:
org.openqa.selenium.ElementClickInterceptedException: element click intercepted: Element <input type="radio" id="yesRadio" name="like" class="custom-control-input"> is not clickable at point (333, 534). Other element would receive the click: <iframe frameborder="0" src="[https://be82b301cd639377a83c1cc02b250d58.safeframe.googlesyndication.com/safeframe/1-0-40/html/container.html](https://be82b301cd639377a83c1cc02b250d58.safeframe.googlesyndication.com/safeframe/1-0-40/html/container.html)" id="google\_ads\_iframe\_/21849154601,22343295815/Ad.Plus-Anchor\_0" title="3rd party ad content" name="" scrolling="no" marginwidth="0" marginheight="0" width="728" height="90" data-is-safeframe="true" sandbox="allow-forms allow-popups allow-popups-to-escape-sandbox allow-same-origin allow-scripts allow-top-navigation-by-user-activation" aria-label="Advertisement" tabindex="0" data-google-container-id="1" style="border: 0px; vertical-align: bottom;" data-load-complete="true"></iframe>
这是完整的代码:
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.edge.EdgeDriver;
public class Assign2 {
WebDriver driver;
@BeforeEach
public void setUp(){
driver= new EdgeDriver();
driver.manage().window().maximize();
driver.navigate().to("https://demoqa.com/radio-button");
}
@Test
public void radioChecker(){
By radio1 = By.id("yesRadio");
driver.findElement(radio1).click();
boolean isSelect1 = driver.findElement(radio1).isSelected();
Assertions.assertTrue(isSelect1);
}
@AfterEach
public void quitTap(){
driver.quit();
}
您的元素位于 iframe 中,因此您应该在与以下位置的任何元素交互之前“进入”iframe:
driver.switchTo().frame("iframeNameOrId");
最后你应该退出 iframe 来触摸 iframe 之外的任何元素。