【问题标题】:Selenium: Element is not clickable at pointSelenium:元素不可点击
【发布时间】:2016-05-23 21:00:28
【问题描述】:

此问题是由于chrome driver 始终位于element 中间的clicks 试图忠实于实际用户所做的事情。 所以我在考虑这种方法:

首先不是定位一个元素然后点击:

driver.fineElement(By.xpath("bla bla")).click()

编写点击WebElement的通用函数:

def clickOnWebElement(WebElement webElement) {
 int counter = 0;
 boolean isClicked = false;

 Thread.sleep(1000);
try {
    while (count < 2 && !isClicked) {

     if (count == 0) {
        webElement.click()
        isClicked = true;
     }     
     else if (count == 1) {
        Actions action = new Actions(driver);
        action.moveToElement(webElement).click().perform();
        isClicked = true;
       }
     else if (count == 2) {
        JavascriptExecutor js =(JavascriptExecutor)driver;
       js.executeScript("window.scrollTo(0,"element.getLocation().x+")");
        webElement.click();
        isClicked = true;
       }
    }
  }
catch(Exception ex) {
    count++;
    Thread.sleep(2000);
  }
}

然后当这个异常发生时尝试不同的点击方式。

您认为这种方法可行吗?

【问题讨论】:

  • 我认为它可以工作,但是假设如果每个 else if 被执行但仍然有问题的元素没有固定位置尝试在每个 else 中添加 Thread.sleep 如果可能有帮助

标签: java selenium


【解决方案1】:

请通过thisstackoverflow 的答案有更好的理解。

更新我们也可以试试

There are many Conditions that we can use withing Webdriver tests.

1. visibilityOf(WebElement element) : An expectation for checking that an element, known 
to be present on the DOM of a page, is visible.
2. visibilityOfElementLocated(By locator) : An expectation for checking that an element 
is present on the DOM of a page and visible.

In the above two conditions we are waiting for an element to be present on the DOM 
of a page and also visible. These works fine only when the element is loaded completely.

也请尝试如下

尝试使用 Y 坐标点击

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

尝试使用 X 坐标点击

WebElement elementToClick = driver.findElement(By.xpath("Your xpath"));
// Scroll the browser to the element's X position
((JavascriptExecutor)driver).executeScript("window.scrollTo(0,"+elementToClick.getLocation().x+")");
// Click the element
elementToClick.click();

希望对你有帮助

【讨论】:

  • 那么对于任何点击错误异常(见我的更新)使用这个函数呢?
  • 其他元素会得到点击错误仅仅意味着元素存在于 DOM 中但仍然没有固定的位置,所以如果你认为你的函数足以处理上述点,那么它很好
  • 以及如何修复这个固定位置?
  • 我们不需要做任何事情来获得元素固定位置它只需要一些时间因此通常我在单击之前执行一些其他操作我也看到使用 css 选择器通常可以解决这个问题(它smy 观察简单)
  • 嗨,请通过我在那里分享的链接我已经提到我们可以执行什么操作
猜你喜欢
  • 2015-06-13
  • 2018-03-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多