此问题是由于 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/