【问题标题】:Selenium Web Driver & Java. Element is not clickable at point (x, y). Other element would receive the clickSelenium Webdriver 和 Java。元素在点 (x, y) 处不可点击。其他元素会收到点击
【发布时间】:2017-12-08 07:04:03
【问题描述】:

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

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);
}

【问题讨论】:

  • 您使用的是 Chrome 61+ 版本吗?
  • @demouser123 我正在使用 Firefox 47.0.1 和 seleniumWebDriver 2.51.0
  • @Maria 您在哪一行收到错误消息?谢谢
  • @DebanjanB 行内:driver.findElement(By.id("navigationPageButton")).click();
  • 该错误意味着,有另一个元素覆盖目标元素(固定/绝对定位覆盖)或 z-index 太低。这可能是由使用过渡的悬停效果引起的(低于最小超时,在本例中为 393 毫秒)。您应该等待 #navigationPageButton 变为可见(或使用 elementToBeClickable() 对该元素也可点击)或检查是否满足所有先决条件以便按钮可点击。

标签: java selenium selenium-webdriver webdriver


【解决方案1】:

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

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

这个异常的字段是:

  • BASE_SUPPORT_URLprotected static final java.lang.String BASE_SUPPORT_URL
  • DRIVER_INFO : public 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 

从您的代码块中可以清楚地看出,您已将wait 定义为WebDriverWait wait = new WebDriverWait(driver, 10);,但您在ExplicitWait 发挥作用之前在元素上调用click() 方法,如until(ExpectedConditions.elementToBeClickable) 中一样。

解决方案

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

1.由于存在 JavaScript 或 AJAX 调用,元素没有被点击

尝试使用Actions类:

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

2。元素没有被点击,因为它不在Viewport

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

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

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

在这种情况下诱导 ExplicitWaitWebDriverWait 如第 4 点所述。

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

在这种情况下,ExplicitWaitExpectedConditions 设置为 elementToBeClickable 以使元素可点击:

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

5.元素存在但具有临时叠加层。

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

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);

【讨论】:

  • 至上面的#6/#2: .ExecuteScript 方法现在可以从 Web 驱动程序本身而不是 JavascriptExecutor 访问。感谢您写得很好的答案!
  • 您已经介绍了许多可能性,其中只有 5 和 6 是处理上述错误的正确方法。前四个抛出不同的错误,您给出的解决方案将不起作用。例如,第 3 点实际上是一个陈旧的元素问题,即使您使用 elementToBeClickble 方法等待多长时间,它也不起作用。这必须以不同的方式处理。
  • 6 并不是真正正确的;这是解决此问题的一种技巧,如果使用正确的预期条件,则 5 将是正确的。 4 看起来是唯一正确的答案。
  • 需要注意的重要一点是,当我们模拟用户的操作时,可能非常不希望使用 javascript 点击根本无法点击的元素(#6)。最终用户永远不会这样做,他们只会滚动到元素以将其带入视口或关闭任何叠加层(如果页面允许)与之交互。
【解决方案2】:

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

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

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

【讨论】:

  • 有效!我无法想象它的工作方式,但否则它会点击覆盖层(等待覆盖层关闭“invisibilityOfElementLocated”大约需要 30 秒。)。
  • 由于我是用java写的,不是熟悉的war,能否请您写完整的解释,请您提供完整的流程吗?
【解决方案3】:

我在尝试单击某个元素(或其叠加层,我不在乎)时遇到了这个错误,而其他答案对我不起作用。我通过使用elementFromPoint DOM API 来修复它,以找到 Selenium 想要我点击的元素:

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()

我也遇到过元素在移动的情况,例如因为页面上它上方的元素正在执行动画展开或折叠。在这种情况下,这个预期条件类会有所帮助。你给它动画元素,而不是你想要点击的元素。此版本仅适用于 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

【讨论】:

    【解决方案4】:

    你可以试试

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

    【讨论】:

    • 这对我没有帮助。
    • 是:org.openqa.selenium.WebDriverException:元素在点 (36, 72) 处不可点击。其他元素会收到点击:
      命令持续时间或超时:70 毫秒
    • 试试下面的WebElement element = driver.findElement(By.id("navigationPageButton")); Actions actions = new Actions(driver); actions.moveToElement(element).click().perform();
    • 这也无济于事。我有两个 Exception 和一个 AssertionError 以及下一个错误“元素在点不可点击”
    • 如果我使用 Thread.Sleep 那么一切正常。但我使用 Wait all failed.
    【解决方案5】:

    将页面滚动到异常中提到的附近点对我来说是诀窍。下面是代码sn-p:

    $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

    【讨论】:

      【解决方案6】:

      如果元素不可点击并且出现覆盖问题,我们使用 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);
      

      【讨论】:

        【解决方案7】:

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

        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;
                }
            }
        }
        

        【讨论】:

          【解决方案8】:

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

          driver.ExecuteJavaScript("arguments[0].checked=true", radio);
          

          【讨论】:

            【解决方案9】:

            可以试试下面的代码

             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();
                }
            

            【讨论】:

              猜你喜欢
              • 2023-03-19
              • 2018-09-30
              • 2017-11-27
              相关资源
              最近更新 更多