Selenium:元素在点上不可点击 [英] Selenium: Element is not clickable at point

查看:1143
本文介绍了Selenium:元素在点上不可点击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题是由于 chrome驱动程序总是点击 元素的中间部分试图忠实于实际用户所做的事情。
所以我想到了这种方法:

This issue is due to the chrome driver always clicks the middle of the element in attempt to be faithful to what an actual user does. So i was thinking of this approach:

首先找到一个元素而不是点击:

First instead of locate an element and click:

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

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

And then when this exception occurs try different way to click.

您认为这种方法可行吗?

You think this approach can work ?

推荐答案

请转到通过这个堆栈溢出答案有更好的理解。

Please go through this stack overflow answer to have a better understanding.

更新我们也可以尝试

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

希望这有助于你

这篇关于Selenium:元素在点上不可点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆