java - Selenium:元素在该点不可点击

标签 java selenium

此问题是由于 chrome 驱动程序 总是单击 元素 的中间,以尝试忠实于实际用户的操作。 所以我正在考虑这种方法:

首先不是找到一个元素并单击:

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

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

您认为这种方法可行吗?

最佳答案

请通过this堆栈溢出答案以更好地理解。

更新我们也可以尝试

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

希望这对你有帮助

关于java - Selenium:元素在该点不可点击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37388866/

相关文章:

java - lambda Java 8,如何映射作为过滤操作结果字段的列表

java - 将当前年份值设置为整数

java - 公开课之间的交流

java - 如何使用 Selenium WebDriver 从变量中提取文本?

javascript - Protractor :无法使用 Protractor 获取所有元素

java - 我可以动态卸载和重新加载(相同的其他版本)JAR 吗?

java - 如何使用 Selenium 和 xpath 定位元素?

java - 使用 Flex Monkey 和 Selenium 自动化 Flex 应用程序的步骤

java - 使用 http 创建 AppiumDriver

java - TestNg 与多个 Factory 进行测试