一尘不染

selenium:元素不可点击

selenium

这个问题是由于chrome driverclicks的中间element在试图忠实于什么实际用户一样。所以我在想这种方法:

首先,而不是查找元素,然后单击:

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

然后,当发生此异常时,请尝试以其他方式单击。

您认为这种方法可行吗?


阅读 418

收藏
2020-06-26

共1个答案

一尘不染

请仔细阅读此堆栈溢出答案以更好地理解。

更新 我们也可以尝试

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

希望这对您有帮助

2020-06-26