Selenium Web 驱动程序 & Java.元素在点 (x, y) 处不可单击。其他元素将收到点击

Selenium Web Driver & Java. Element is not clickable at point (x, y). Other element would receive the click

提问人:Maria 提问时间:7/5/2017 最后编辑:Bruno BieriMaria 更新时间:8/1/2021 访问量:216736

问:

我使用了显式等待,并收到警告:

org.openqa.selenium.WebDriverException: 元素在点 (36, 72) 处不可单击。其他元素将收到 咔嚓咔嚓:...... 命令持续时间或超时:393 毫秒

如果我使用,我不会收到任何警告。Thread.sleep(2000)

@Test(dataProvider = "menuData")
public void Main(String btnMenu, String TitleResultPage, String Text) throws InterruptedException {
    WebDriverWait wait = new WebDriverWait(driver, 10);
    driver.findElement(By.id("navigationPageButton")).click();

    try {
       wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector(btnMenu)));
    } catch (Exception e) {
        System.out.println("Oh");
    }
    driver.findElement(By.cssSelector(btnMenu)).click();
    Assert.assertEquals(driver.findElement(By.cssSelector(TitleResultPage)).getText(), Text);
}
java 硒 webdriver webdriver

评论

0赞 demouser123 7/5/2017
您使用的是 Chrome 版本 61+ 吗?
0赞 Maria 7/5/2017
@demouser123我正在使用 Firefox 47.0.1 和 seleniumWebDriver 2.51.0
0赞 undetected Selenium 7/5/2017
@Maria 您在哪一行收到错误?谢谢
0赞 Maria 7/5/2017
@DebanjanB 内联: driver.findElement(By.id(“navigationPageButton”)).click();
0赞 try-catch-finally 7/5/2017
该错误意味着,有另一个元素覆盖了目标元素(固定/绝对位置叠加)或 z 指数太低。这可能是由使用过渡的悬停效果引起的(慢于最小超时,在本例中为 393 毫秒)。您应该等待变得可见(或也可以使用该元素单击)或检查是否满足所有先决条件,以便按钮可单击。#navigationPageButtonelementToBeClickable()

答:

2赞 fg78nc 7/5/2017 #1

你可以试试

WebElement navigationPageButton = (new WebDriverWait(driver, 10))
 .until(ExpectedConditions.presenceOfElementLocated(By.id("navigationPageButton")));
navigationPageButton.click();

评论

0赞 Maria 7/5/2017
这对我没有帮助。
0赞 Maria 7/5/2017
是:org.openqa.selenium.WebDriverException:元素在点 (36, 72) 处不可单击。其他元素将收到单击:<div tabindex=“0” class=“waiter-ui-lock”></div> 命令持续时间或超时:70 毫秒
1赞 fg78nc 7/5/2017
请尝试以下操作WebElement element = driver.findElement(By.id("navigationPageButton")); Actions actions = new Actions(driver); actions.moveToElement(element).click().perform();
0赞 Maria 7/5/2017
这也无济于事。我有两个 Exception 和一个 AssertionError,接下来是一些错误“元素在点上不可点击”
1赞 Maria 7/5/2017
如果我使用 Thread.Sleep,那么一切正常。但是我使用Wait all失败了。
210赞 undetected Selenium 7/5/2017 #2

WebDriverException:元素在点 (x, y) 处不可单击

这是一个典型的 org.openqa.selenium.WebDriverException,它扩展了 java.lang.RuntimeException

此异常的字段为:

  • BASE_SUPPORT_URLprotected static final java.lang.String BASE_SUPPORT_URL
  • DRIVER_INFOpublic static final java.lang.String DRIVER_INFO
  • SESSION_IDpublic static final java.lang.String SESSION_ID

关于您的个人用例,错误说明了一切:

WebDriverException: Element is not clickable at point (x, y). Other element would receive the click 

从您的代码块中可以清楚地看出,您已经定义了 as,但是在 中 开始发挥作用之前,您正在对元素调用该方法。waitWebDriverWait wait = new WebDriverWait(driver, 10);click()ExplicitWaituntil(ExpectedConditions.elementToBeClickable)

溶液

错误可能由不同的因素引起。您可以通过以下任一过程来解决这些问题:Element is not clickable at point (x, y)

1. 由于存在 JavaScript 或 AJAX 调用,元素未被单击

尝试使用 Class:Actions

WebElement element = driver.findElement(By.id("navigationPageButton"));
Actions actions = new Actions(driver);
actions.moveToElement(element).click().build().perform();

2. 元素未被单击,因为它不在视口

尝试使用 JavascriptExecutor 将元素引入视口:

WebElement myelement = driver.findElement(By.id("navigationPageButton"));
JavascriptExecutor jse2 = (JavascriptExecutor)driver;
jse2.executeScript("arguments[0].scrollIntoView()", myelement); 

3. 在元素可点击之前,页面正在刷新。

在这种情况下,请诱导 ExplicitWait,即 WebDriverWait,如第 4 点所述。

4. 元素存在于 DOM 中,但不可点击。

在本例中,请将 ExplicitWait 设置为,使元素可单击:ExpectedConditionselementToBeClickable

WebDriverWait wait2 = new WebDriverWait(driver, 10);
wait2.until(ExpectedConditions.elementToBeClickable(By.id("navigationPageButton")));

5. 元素存在,但具有临时覆盖。

在这种情况下,诱导 ExplicitWait,并将 ExpectedConditions 设置为 invisibilityOfElementLocated 以使覆盖不可见。

WebDriverWait wait3 = new WebDriverWait(driver, 10);
wait3.until(ExpectedConditions.invisibilityOfElementLocated(By.xpath("ele_to_inv")));

6. 元素存在,但具有永久覆盖。

用于直接在元素上发送单击。JavascriptExecutor

WebElement ele = driver.findElement(By.xpath("element_xpath"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", ele);

评论

1赞 Tabrock 5/16/2018
到上面的 #6/#2:.现在可以从 Web 驱动程序本身而不是 JavascriptExecutor 访问 ExecuteScript 方法。感谢您写得很好的答案!
8赞 Rajagopalan 8/7/2018
您已经介绍了许多可能性,其中只有 5 和 6 是处理上述错误的正确方法。前四个会引发不同的错误,您给出的解决方案将不起作用。例如,第 3 点实际上是一个过时的元素问题,即使您使用 elementToBeClickble 方法等待多长时间,它也不起作用。这必须以不同的方式处理。
0赞 Ardesco 3/10/2019
6 不是真的正确它;这是一个解决该问题的黑客,如果使用了正确的预期条件,则 5 将是正确的。4 看起来是唯一正确的答案。
2赞 Praveen Tiwari 2/24/2020
需要注意的重要一点是,当我们模拟用户的操作时,使用 javascript 单击根本无法单击的元素 (#6) 可能是非常不可取的。最终用户永远不会这样做,他们只需滚动到元素以将其带入视口,或者关闭任何覆盖层(如果页面允许)以与之交互。
21赞 Rester Test 4/13/2018 #3

如果您需要将其与 Javascript 一起使用

我们可以使用 arguments[0].click() 来模拟点击操作。

var element = element(by.linkText('webdriverjs'));
browser.executeScript("arguments[0].click()",element);

评论

0赞 Fisk 6/1/2018
工程!我无法想象它的工作原理,但除此之外,它会点击覆盖层(等待“invisibilityOfElementLocated”关闭覆盖层大约需要 30 秒)。
0赞 Bastian 10/23/2019
你能写完整的解释吗,因为我是用java写的,而且不熟悉战争,你能提供完整的流程吗?
4赞 rescdsk 7/4/2018 #4

我在尝试单击某些元素(或其覆盖层,我不在乎)时遇到了此错误,而其他答案对我不起作用。我通过使用 DOM API 来查找 Selenium 希望我点击的元素来修复它:elementFromPoint

element_i_care_about = something()
loc = element_i_care_about.location
element_to_click = driver.execute_script(
    "return document.elementFromPoint(arguments[0], arguments[1]);",
    loc['x'],
    loc['y'])
element_to_click.click()

我也遇到过元素移动的情况,例如,因为页面上它上面的元素正在进行动画展开或折叠。在这种情况下,这个 Expected Condition 类很有帮助。你给它提供动画元素,而不是你想点击的元素。此版本仅适用于 jQuery 动画。

class elements_not_to_be_animated(object):
    def __init__(self, locator):
        self.locator = locator

    def __call__(self, driver):
        try:
            elements = EC._find_elements(driver, self.locator)
            # :animated is an artificial jQuery selector for things that are
            # currently animated by jQuery.
            return driver.execute_script(
                'return !jQuery(arguments[0]).filter(":animated").length;',
                elements)
        except StaleElementReferenceException:
            return False
2赞 Sudheesh.M.S 8/1/2018 #5

将页面滚动到异常中提到的附近点对我来说很成功。下面是代码片段:

$wd_host = 'http://localhost:4444/wd/hub';
$capabilities =
    [
        \WebDriverCapabilityType::BROWSER_NAME => 'chrome',
        \WebDriverCapabilityType::PROXY => [
            'proxyType' => 'manual',
            'httpProxy' => PROXY_DOMAIN.':'.PROXY_PORT,
            'sslProxy' => PROXY_DOMAIN.':'.PROXY_PORT,
            'noProxy' =>  PROXY_EXCEPTION // to run locally
        ],
    ];
$webDriver = \RemoteWebDriver::create($wd_host, $capabilities, 250000, 250000);
...........
...........
// Wait for 3 seconds
$webDriver->wait(3);
// Scrolls the page vertically by 70 pixels 
$webDriver->executeScript("window.scrollTo(0, 70);");

注意:我使用 Facebook php webdriver

0赞 user2274204 11/3/2019 #6

最好的解决方案是覆盖点击功能:

public void _click(WebElement element){
    boolean flag = false;
    while(true) {
        try{
            element.click();
            flag=true;
        }
        catch (Exception e){
            flag = false;
        }
        if(flag)
        {
            try{
                element.click();
            }
            catch (Exception e){
                System.out.printf("Element: " +element+ " has beed clicked, Selenium exception triggered: " + e.getMessage());
            }
            break;
        }
    }
}
0赞 Ayub 1/20/2020 #7

在 C# 中,我在检查时遇到了问题, 这对我有用:RadioButton

driver.ExecuteJavaScript("arguments[0].checked=true", radio);
0赞 Nagarjuna Yalamanchili 1/29/2020 #8

可以尝试使用以下代码

 WebDriverWait wait = new WebDriverWait(driver, 30);

传递其他元素将收到点击<a class="navbar-brand" href="#"></a>

    boolean invisiable = wait.until(ExpectedConditions
            .invisibilityOfElementLocated(By.xpath("//div[@class='navbar-brand']")));

传递可点击的按钮 ID,如下所示

    if (invisiable) {
        WebElement ele = driver.findElement(By.xpath("//div[@id='button']");
        ele.click();
    }
1赞 sweta kumari 12/15/2020 #9

如果元素不可点击并且存在覆盖问题,则使用 arguments[0].click()。

WebElement ele = driver.findElement(By.xpath("//div[@class='input-group-btn']/input"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", ele);