解决Selenium报错:ElementClickInterceptedException – Element Click Intercepted – Not Clickable At Point – Other Element Would Receive The Click

2021年11月7日17:57:04 发表评论 1,511 次浏览

Selenium ElementClickInterceptedException – Selenium 脚本中非常常见的异常,我们尝试在不知道发生原因的情况下解决此异常。人们等待或等待元素可点击或非常常见的 JavaScript。下面看看我们如何解决ElementClickInterceptedException Element Click Intercepted。

让我们在一个场景中复制相同的异常:-

  1. 打开谷歌。
  2. 在搜索框中输入“Facebook”。
  3. 单击搜索按钮。
  4. 验证页面标题。

Selenium Element Click Intercepted - 简单的步骤和以下是代码片段:-

package BasicSeleniumConcepts;

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.testng.annotations.Test;

import io.github.bonigarcia.wdm.WebDriverManager;

public class ClickInterceptedException {

	@Test
	public void googleSearch() {
		WebDriverManager.chromedriver().setup();
		WebDriver driver = new ChromeDriver();
		driver.get("https://www.google.com");
		// Typing search keyword
		driver.findElement(By.name("q")).sendKeys("facebook");
		// Locating search button and click on it
		WebElement ele = driver.findElement(By.xpath("(//input[@name='btnK'])[2]"));
		ele.click();	Assert.assertTrue(ExpectedConditions.titleContains("facebook").apply(driver).booleanValue());
	}
}

输出

org.openqa.selenium.ElementClickInterceptedException: element click intercepted: 
Element <input class="gNO89b" value="Google Search" aria-label="Google Search" name="btnK" type="submit" data-ved="0ahUKEwjV3PTVhczpAhVZzTgGHQxWCfkQ4dUDCAs"> 
is not clickable at point (432, 368). Other element would receive the click: <span>...</span>

无法单击“搜索”按钮。让我们不了解发生这种情况的原因并使用我们的选项。

解决ElementClickInterceptedException Element Click Intercepted:使用一些等待

package BasicSeleniumConcepts;

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.testng.annotations.Test;

import io.github.bonigarcia.wdm.WebDriverManager;

public class ClickInterceptedException {

	@Test
	public void googleSearch() throws InterruptedException {
		WebDriverManager.chromedriver().setup();
		WebDriver driver = new ChromeDriver();
		driver.get("https://www.google.com");
		// Typing search keyword
		driver.findElement(By.name("q")).sendKeys("facebook");
		Thread.sleep(10000);
		// Locating search button and click on it
		WebElement ele = driver.findElement(By.xpath("(//input[@name='btnK'])[2]"));
		ele.click();
Assert.assertTrue(ExpectedConditions.titleContains("facebook").apply(driver).booleanValue());
	}
}

输出再次相同。

Selenium ElementClickInterceptedException:使用等待元素可点击

package BasicSeleniumConcepts;

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 org.testng.annotations.Test;

import io.github.bonigarcia.wdm.WebDriverManager;

public class ClickInterceptedException {

	@Test
	public void googleSearch() {
		WebDriverManager.chromedriver().setup();
		WebDriver driver = new ChromeDriver();
		driver.get("https://www.google.com");
		// Typing search keyword
		driver.findElement(By.name("q")).sendKeys("facebook");
		// Locating search button and click on it
		WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(30));
		WebElement ele = driver.findElement(By.xpath("(//input[@name='btnK'])[2]"));
		wait.until(ExpectedConditions.elementToBeClickable(ele));
		ele.click();
 Assert.assertTrue(ExpectedConditions.titleContains("facebook").apply(driver).booleanValue());
	}
}

输出再次相同。

使用 JavaScript 点击

package BasicSeleniumConcepts;

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
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.testng.Assert;
import org.testng.annotations.Test;

import io.github.bonigarcia.wdm.WebDriverManager;

public class ClickInterceptedException {

	@Test
	public void googleSearch() {
		WebDriverManager.chromedriver().setup();
		WebDriver driver = new ChromeDriver();
		driver.get("https://www.google.com");
		// Typing search keyword
		driver.findElement(By.name("q")).sendKeys("facebook");
		// Locating search button and click on it
		WebElement ele = driver.findElement(By.xpath("(//input[@name='btnK'])[2]"));
		JavascriptExecutor jse = (JavascriptExecutor)driver;
		jse.executeScript("arguments[0].click()", ele);
		Assert.assertTrue(ExpectedConditions.titleContains("facebook").apply(driver).booleanValue());
	}
}

有效。

使用 JavaScript 单击它可以工作,但我们绕过了可能是潜在错误或糟糕的 UX 设计的实际原因。当我们输入搜索关键字时,谷歌会显示自动建议并隐藏搜索按钮。根据 Selenium 官方文档 – ElementClickInterceptedException 表示无法正确执行单击,因为目标元素以某种方式被遮挡。此异常类扩展了 ElementNotInteractableException 类。

解决Selenium报错:ElementClickInterceptedException – Element Click Intercepted – Not Clickable At Point – Other Element Would Receive The Click

Selenium Element Click Intercepted:这就是 Selenium Click 无法点击元素的原因。搜索按钮与自动建议选项重叠。异常详细信息还显示哪个元素将被点击而不是搜索按钮。这就是等待既不工作也不等待可点击的原因。JavaScript 直接在 DOM 级别工作,因此可以单击。

这可能是一个糟糕的设计,因为它隐藏了搜索按钮。第一次使用 google 的人可能不知道他可以使用 Enter 键或单击自动建议列表中的任何建议来允许搜索。如果这是行为,请将 Google 放在一边并考虑你的应用程序。

此异常背后可能有多种原因。以上只是原因之一。如果你了解导致这种情况的原因,而不是盲目地使用 JavaScript 并绕过潜在的错误,你就可以编写更好的处理代码。

以上场景异常可以通过以下方式解决ElementClickInterceptedException Element Click Intercepted:-

  1. 通过将 Enter 键发送到搜索按钮而不是单击。
  2. 通过单击自动建议中的匹配选项。

通过将 Enter 键发送到“搜索”按钮而不是单击

package BasicSeleniumConcepts;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
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.testng.Assert;
import org.testng.annotations.Test;

import io.github.bonigarcia.wdm.WebDriverManager;

public class ClickInterceptedException {

	@Test
	public void googleSearch() {
		WebDriverManager.chromedriver().setup();
		WebDriver driver = new ChromeDriver();
		driver.get("https://www.google.com");
		// Typing search keyword
		driver.findElement(By.name("q")).sendKeys("facebook");
		// Locating search button and click on it
		WebElement ele = driver.findElement(By.xpath("(//input[@name='btnK'])[2]"));
		ele.sendKeys(Keys.ENTER);
		Assert.assertTrue(ExpectedConditions.titleContains("facebook").apply(driver).booleanValue());
	}
}

通过单击自动建议中的匹配选项

package BasicSeleniumConcepts;

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 org.testng.Assert;
import org.testng.annotations.Test;

import io.github.bonigarcia.wdm.WebDriverManager;

public class ClickInterceptedException {

	@Test
	public void googleSearch() {
		WebDriverManager.chromedriver().setup();
		WebDriver driver = new ChromeDriver();
		driver.get("https://www.google.com");
		// Typing search keyword
		driver.findElement(By.name("q")).sendKeys("facebook");
		// Locating first suggestion button and click on it. Why first suggestion bcz whatever we type hat will come always at first
		WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(5));
		WebElement ele = wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("(//ul/li)[1]")));
		ele.click();
		Assert.assertTrue(ExpectedConditions.titleContains("facebook").apply(driver).booleanValue());
	}
}

以上就是解决Selenium ElementClickInterceptedException的全部内容和分析,你可以从这里下载/克隆上面的示例项目 。

木子山

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: