“Element is not clickable at point because another element obscures it.”

huangapple go评论72阅读模式
英文:

Element is not clickable at point because another element obscures it

问题

我正在使用Selenium WebDriver,但是我遇到了以下异常:

    org.openqa.selenium.ElementClickInterceptedException: 元素<div id="nav-icon3">在坐标点(21,37) 处无法被点击,因为另一个元素<div class="loader-section section-left"> 遮挡了它。
    构建信息:版本:'3.141.59',修订版本:'e82be7d358',时间:'...
    驱动信息:org.openqa.selenium.firefox.FirefoxDriver
    
    会话ID:868f9daa-dd6c-4b53-846d-7323e7b0408e
    	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    	at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)

以下是我的代码。

    driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
    WebElement element = driver.findElement(By.id("nav-icon3"));
    JavascriptExecutor executor = (JavascriptExecutor)driver;
    executor.executeScript("arguments[0].click()", element);
    driver.findElement(By.id("nav-icon3")).click();
    driver.findElement(By.xpath("//ul[@id='slide-out']/li/ul/li[2]/a")).click();
    driver.findElement(By.xpath("//ul[@id='slide-out']/li/ul/li[2]/div/ul/li/a")).click();
    driver.findElement(By.id("newTravelerLink")).click();
英文:

I am using Selenium webdriver and i got the following exception

org.openqa.selenium.ElementClickInterceptedException: Element <div id="nav-icon3"> is not clickable at point (21,37) because another element <div class="loader-section section-left"> obscures it
Build info: version: '3.141.59', revision: 'e82be7d358', time: '
Driver info: org.openqa.selenium.firefox.FirefoxDriver

Session ID: 868f9daa-dd6c-4b53-846d-7323e7b0408e
	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)

My code is given below.

driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
	  WebElement element = driver.findElement(By.id("nav-icon3"));
	  JavascriptExecutor executor = (JavascriptExecutor)driver;
	  executor.executeScript("arguments[0].click()", element);
	driver.findElement(By.id("nav-icon3")).click();
      driver.findElement(By.xpath("//ul[@id='slide-out']/li/ul/li[2]/a")).click();
	  driver.findElement(By.xpath("//ul[@id='slide-out']/li/ul/li[2]/div/ul/li/a")).click();
	  driver.findElement(By.id("newTravelerLink")).click();

答案1

得分: 1

我在制作adFly机器人时遇到了这个问题。

使用等待或类似的方法通常是无用的,不要过于依赖使用elementobeclickable()方法。

唯一解决方法是获取该位置上的所有元素,并从页面中删除除了您感兴趣的元素之外的所有元素。

步骤1:

获取页面上每个diviframespan的X和Y坐标点。
首先尝试这三个WebElements(通过循环使用PointgetX()getY()方法,
(https://www.programcreek.com/java-api-examples/?class=org.openqa.selenium.Point&method=getX),
将它们都放入一个名为overlappingElementsArrayList中,
然后从ArrayList中移除被遮挡的元素。

步骤2:

在另一个循环中删除每个与您的WebElement重叠的WebElement

JavaScriptExecutor jsExecutor = (JavaScriptExecutor) driver;

for(int i = 0; i < overlappingElements.size(); i++) {

jsExecutor.executeScript(
    "arguments[0].parentNode.removeChild(arguments[0])", overlappingElements.get(i));

}

步骤3:只需对obscuredElement使用click()方法。

英文:

I've encountered this problem while doing an adFly bot.

Using waits or similar methods is often useless, don't rely on using elementobeclickable() too.

The only way to get around it is to get all the element at that position and remove all of those from the page except the one you're interested in.

Step 1:

Get the X and Y Points of EVERY div, iframe, span in the page.
Try out these three WebElements first (using getX() and getY() methods of Point
(https://www.programcreek.com/java-api-examples/?class=org.openqa.selenium.Point&method=getX) through a cycle.

put them all in an ArrayList called let's say, overlappingElements,

then remove from the ArrayList the obscuredElement.

Step 2:

Remove every WebElement that overlap over your WebElement in another cycle.

JavaScriptExecutor jsExecutor = (JavaScriptExecutor) driver;

for(int i = 0; i < overlappingElements.size(); i++) {

jsExecutor.executeScript(
    "arguments[0].parentNode.removeChild(arguments[0])", overlappingElements.get(i));

}

Step 3: Just click() on obscuredElement.

答案2

得分: 0

以下是已翻译的内容:

如果您编写了一个等待方法,您可以在项目中使用该等待方法

private static WebElement waitForElement(By locator, int timeout)
{
    WebElement element=new WebDriverWait(driver,timeout).until(ExpectedConditions.presenceOfElementLocated(locator));
    return element;
}

上述代码是等待方法

如果您想使用这个方法


waitForElement(By.xpath("//button[@type='submit']"),50);
例如,您可以在这里使用您的网页元素。

英文:

If you code a method to wait you can use that wait method in your project

private static WebElement waitForElement(By locator, int timeout)
{
    WebElement element=new WebDriverWait(driver,timeout).until(ExpectedConditions.presenceOfElementLocated(locator));
    return element;
}

The above code is wait method

If you want to use this method


waitForElement(By.xpath("//button[@type='submit']"),50);
This for example ,you can use your web element here

答案3

得分: 0

错误信息...

org.openqa.selenium.ElementClickInterceptedException: 元素<div id="nav-icon3">在点(21,37) 处不可点击,因为另一个元素<div class="loader-section section-left">遮挡了它

...意味着元素<div id="nav-icon3">无法被点击,因为元素<div class="loader-section section-left">遮挡了它。


解决方法

click()元素,您需要引入WebDriverWait等待elementToBeClickable(),并且您可以使用以下任一定位策略

  • cssSelector

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("div#nav-icon3"))).click();
    
  • xpath

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[@id='nav-icon3']"))).click();
    

如果click()仍然失败,则需要为invisibilityOf()引入WebDriverWait,如下所示:

  • cssSelector

    new WebDriverWait(driver, 20).until(ExpectedConditions.invisibilityOf(driver.findElement(By.cssSelector("div.loader-section.section-left"))));
    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("div#nav-icon3"))).click();
    
  • xpath

    new WebDriverWait(driver, 20).until(ExpectedConditions.invisibilityOf(driver.findElement(By.xpath("//div[@class='loader-section section-left']"))));
    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[@id='nav-icon3']"))).click();
    

或者对于invisibilityOfElementLocated(),如下所示:

  • cssSelector

    new WebDriverWait(driver, 20).until(ExpectedConditions.invisibilityOfElementLocated(By.cssSelector("div.loader-section.section-left")));
    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("div#nav-icon3"))).click();
    
  • xpath

    new WebDriverWait(driver, 20).until(ExpectedConditions.invisibilityOfElementLocated(By.xpath("//div[@class='loader-section section-left']")));
    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[@id='nav-icon3']"))).click();
    

参考资料

您可以在以下链接中找到几篇相关的详细讨论:

英文:

This error message...

org.openqa.selenium.ElementClickInterceptedException: Element <div id="nav-icon3"> is not clickable at point (21,37) because another element <div class="loader-section section-left"> obscures it

...implies that the element <div id="nav-icon3"> can't be clicked as the element <div class="loader-section section-left"> obsecures it.


Solution

To click() on the element you need to induce WebDriverWait for the elementToBeClickable() and you can use either of the following Locator Strategies:

  • cssSelector:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("div#nav-icon3"))).click();
    
  • xpath:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[@id='nav-icon3']"))).click();
    

Incase the click() still fails you need to induce WebDriverWait either for invisibilityOf() as follows:

  • cssSelector:

    new WebDriverWait(driver, 20).until(ExpectedConditions.invisibilityOf(driver.findElement(By.cssSelector("div.loader-section.section-left"))));
    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("div#nav-icon3"))).click();
    
  • xpath:

    new WebDriverWait(driver, 20).until(ExpectedConditions.invisibilityOf(driver.findElement(By.xpath("//div[@class='loader-section section-left']"))));
    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[@id='nav-icon3']"))).click();
    

Or for invisibilityOfElementLocated() as follows:

  • cssSelector:

    new WebDriverWait(driver, 20).until(ExpectedConditions.invisibilityOfElementLocated(By.cssSelector("div.loader-section.section-left")));
    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("div#nav-icon3"))).click();
    
  • xpath:

    new WebDriverWait(driver, 20).until(ExpectedConditions.invisibilityOfElementLocated(By.xpath("//div[@class='loader-section section-left']")));
    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[@id='nav-icon3']"))).click();
    

Reference

You can find a couple of relevant detailed discussions in:

huangapple
  • 本文由 发表于 2020年9月14日 17:38:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/63881801.html
匿名

发表评论

匿名网友

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

确定