【问题标题】:Selenium - Element is not clickable at pointSelenium - 元素不可点击
【发布时间】:2015-06-13 00:38:57
【问题描述】:

我正在使用 selenium 作为测试脚本。我收到以下错误,并且此错误随机发生。当我跑 10 次时,我得到了大约两次。所以它不是真正可复制的。有谁知道为什么会这样?我尝试单击的元素在浏览器中绝对可见并且不会四处移动,因此无需调整大小或拖动元素。我正在使用 chrome webdriver,并且阅读了其他故障排除策略(Debugging "Element is not clickable at point" error),它们似乎与我的问题无关。我也等了足够的时间。

UnknownError: unknown error: Element is not clickable at point (167, 403). Other element would receive the click: <div class="leftMasterBackground"></div>

【问题讨论】:

  • 你能给我们一些关于你想点击什么元素的信息吗?它周围有哪些元素以及在点击之前执行了哪些操作也会很有趣。
  • 使用显式等待,直到元素变为可见点击。
  • 出现此错误时页面上的所有脚本和样式是否已加载完毕?
  • 当页面加载时元素被加载,从技术上讲,应该不需要等待时间来检查元素的存在(检查这个元素是在其他人的一些测试之后发生的)我检查了截图是在页面加载时拍摄的,我可以直观地看到页面上加载的元素,因此 selenium 没有理由无法检测到该元素。我想要做的是单击元素(按钮)。这是非常奇怪和烦人的,因为它是随机发生的。有谁知道元素 selenium clicks 在哪里?

标签: javascript selenium selenium-chromedriver


【解决方案1】:

您可以执行许多步骤来提高单击不同 UI 元素时的稳定性:

  • 显式等待它在 DOM 中存在
  • 滚动进入元素视图
  • 检查是否可点击

对稳定性有帮助吗?

WebDriverWait wait = new WebDriverWait(driver, 3)
JavascriptExecutor js = ((JavascriptExecutor) driver)

//presence in DOM
wait.until(ExpectedConditions.presenceOfElement(By.id("ID")));

//scrolling
WebElement element = driver.findElement(By.id("ID")));  
js.executeScript("arguments[0].scrollIntoView(true);", element);

//clickable
wait.until(ExpectedConditions.elementToBeClickable(By.id("ID")));

此外,如果您决定用更自定义的界面覆盖默认的Actions 界面,您可以使用两种类型的点击(例如):click() 将具有所有这些稳定性步骤,fastClick() 将是默认点击,没有任何变化。

【讨论】:

  • 我只在我的问题中使用了 //scrolling 块并且它有效。谢谢。可能有一个更干燥的解决方案,使这个函数在你的量角器测试中重复调用。我确信有很多地方需要滚动到页面上的某个位置才能点击。
  • @JCC 在我们的解决方案中,这些原生 selenium 库包含在额外的抽象级别中。所以我们有这样的方法:element.scrollToElement().click(), element.waitUntilPresence().click() 等等。
  • 干得好!关键是滚动!
【解决方案2】:

我已经通过捕获异常并像这样管理它来解决:

        WebDriver driver = new ChromeDriver();
        WebElement element = driver.findElement(By.id("ID"));
        boolean clicked = false;
        do{
            try {
                element.click();
            } catch (WebDriverException e) {
                continue;
            } finally {
                clicked = true;
            }
        } while (!clicked);

【讨论】:

  • 看看ExpectedConditions.isClickable()
  • 我已经看到了这个功能,但是它不能很好地与 chromedriver 一起使用。发生的情况是:条件返回 true,但该元素无法有效点击。
  • 我遇到了这个错误,它把我带到了这个页面。您的元素是否被另一个(透明)元素覆盖?是动画的吗?是否可以使用.executeScript(或您的绑定的等效项)注入 JS 以单击元素?
【解决方案3】:

Chrome 也遇到了同样的问题。我已经解决了在单击元素之前放置一行代码的问题:

scrollToViewElement(driver,xpath);

【讨论】:

    【解决方案4】:

    单击要单击的元素的父元素。这只能是临时解决方案。

    【讨论】:

      【解决方案5】:

      为获得最佳解决方案,请使用 java 脚本来聚焦元素 使用----> JavascriptExecutor jsnew=(JavascriptExecutor) 驱动; WebElement element=driver.findElement(By.xpath("")); jsnew.executeScript("arguments[0].scrollIntoView({block:\"center\"});", element);

      您可以使用 id 、 css 选择器代替 xpath : 此 scrollIntoView 会将这个特定元素带到页面中间,驱动程序将能够点击该元素。

      如果是普通按钮或链接, 采用 jsnew.executeScript("arguments[0].click();",element);

      这是点击的一致解决方案。

      【讨论】:

        【解决方案6】:

        由于微调器之一隐藏了元素,我遇到了同样的问题。

        我给了 xpath,它解决了这个问题。其他人建议 1. 滚动 2. 睡眠也对他们有用。

        【讨论】:

        • 您也可以使用等待条件解决以下错误:waitUntilTrueOrTimeout(ExpectedConditions.invisibilityOfElementLocated(elementHidingOtherElement)); protected V waitUntilTrueOrTimeout(final ExpectedCondition isTrue) { return waitUntilTrueOrTimeout(isTrue, waitTimeOutSeconds); } protected V waitUntilTrueOrTimeout(final ExpectedCondition isTrue, final int waitTimeSeconds) { return new WebDriverWait(this.driver, waitTimeSeconds).ignoring(StaleElementReferenceException.class).until(isTrue);
        【解决方案7】:
        • 这只发生在 chrome 上,所以它适用于 ie 和 firefox
        • ChromeDriver 总是点击元素的中间
        • Chrome 驱动程序没有计算出正确的链接屏幕位置的原因。

        解决方案:

        // Find an element and define it
        
        WebElement elementToClick = driver.findElement(By.xpath("some xpath"));
        // Scroll the browser to the element's Y position
        ((JavascriptExecutor) driver).executeScript("window.scrollTo(0,"+elementToClick.getLocation().y+")");
        // Click the element
        elementToClick.click();
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2018-03-14
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-02-23
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多