Java Selenium WebDriver-元素在点处不可单击,为什么?
我知道关于这个话题有好几条线索,但我不一定在寻找解决方案,而是在寻找解释。我在一个非常大的自动化套件上工作,该套件使用browserstack通过手机测试web应用程序。我的稳定性很低。。这是因为我犯了这个错误!有时它会起作用,有时它不会。。我无法使用操作,因为Browserstack不支持该操作。。为什么会存在这个错误?有没有人成功地解决了这个问题。我总是使用wait.until(ExpectedConditions)来等待对象,但有时这样做不够好。由于这是一个未知的错误,我不能完全理解它是一个例外。此外,我们的标准不允许使用Thread.sleep()。有什么想法吗?多谢各位 下面是一些代码的屏幕。。Java Selenium WebDriver-元素在点处不可单击,为什么?,java,selenium,automation,Java,Selenium,Automation,我知道关于这个话题有好几条线索,但我不一定在寻找解决方案,而是在寻找解释。我在一个非常大的自动化套件上工作,该套件使用browserstack通过手机测试web应用程序。我的稳定性很低。。这是因为我犯了这个错误!有时它会起作用,有时它不会。。我无法使用操作,因为Browserstack不支持该操作。。为什么会存在这个错误?有没有人成功地解决了这个问题。我总是使用wait.until(ExpectedConditions)来等待对象,但有时这样做不够好。由于这是一个未知的错误,我不能完全理解它是一
这是由于selenium的运行速度。它将在页面加载之前尝试查找元素,从而导致此错误 您正在等待WebElement可单击,然后再次查找WebElement列表并单击第一个元素。 这并不保证您正在单击等待它的元素是可单击的
public void waitAndClickElement(WebElement element) {
driverWait.until(ExpectedConditions.elementToBeClickable(element)).click();
}
就你而言
public void clickImageView() {
driverWait.until(ExpectedConditions.elementToBeClickable(listImageView)).click() ;
}
对于您提供的代码示例,
.until()
返回您正在等待的WebElement
。您可以使用下面的代码单击它,而不必再次刮擦页面
public void clickImageView()
{
driverWait.until(ExpectedConditions.elementToBeClickable(listImageView)).click();
}
由于以下原因,元素通常无法单击
public static void waitForElementPresent(final By By,int timeout,WebDriver driver)
在此之后,如果您使用的是浏览器,而不是chrome,则调用滚动到该对象,这将解决您的问题
代码
public static void waitForElementPresent(final By by, int timeout,WebDriver driver) {
waitForPageLoad(driver);
WebDriverWait wait = (WebDriverWait)new WebDriverWait(driver,40).ignoring(StaleElementReferenceException.class);
/* wait.until(new ExpectedCondition<Boolean>(){
@Override
public Boolean apply(WebDriver webDriver) {
WebElement element = webDriver.findElement(by);
return element != null && element.isDisplayed();
}
}); */
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
wait.until(ExpectedConditions.presenceOfElementLocated(by));
wait.until(ExpectedConditions.elementToBeClickable(by));
WebDriverWait wait2 = new WebDriverWait(driver, 40);
wait2.until(ExpectedConditions.elementToBeClickable(by));
}
//wait for page to laod
public static void waitForPageLoad(WebDriver driver) {
ExpectedCondition<Boolean> pageLoadCondition = new
ExpectedCondition<Boolean>() {
public Boolean apply(WebDriver driver) {
return ((JavascriptExecutor)driver).executeScript("return document.readyState").equals("complete");
}
};
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(pageLoadCondition);
}
public static void waitForElementPresent(final By By,int timeout,WebDriver driver){
waitForPageLoad(驱动程序);
WebDriverWait wait=(WebDriverWait)新建WebDriverWait(驱动程序,40)。忽略(StaleElementReferenceException.class);
/*等待.until(新的ExpectedCondition(){
@凌驾
公共布尔应用(WebDriver WebDriver){
WebElement=webDriver.findElement(by);
返回元素!=null&&element.isDisplayed();
}
}); */
试一试{
睡眠(1000);
}捕捉(中断异常e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
等待。直到(预期条件。元素的存在被定位);
等待.直到(预期条件.元素可选择(由));
WebDriverWait wait2=新的WebDriverWait(驱动程序,40);
等待2.直到(预期条件。元素可取消(由));
}
//等待页面刷新
公共静态void waitForPageLoad(WebDriver驱动程序){
ExpectedCondition pageLoadCondition=新建
期望条件(){
公共布尔应用(WebDriver驱动程序){
return((JavascriptExecutor)driver.executeScript(“return document.readyState”).equals(“complete”);
}
};
WebDriverWait wait=新的WebDriverWait(驱动程序,30);
等待.直到(pageLoadCondition);
}
您能为我们发布一些故障跟踪吗?或者任何代码?这也可能是一个没有实际意义的问题,但是你需要使用BrowserStack吗?@jagdpanzer,我更新了这篇文章给你一个例子。目前这是我们最好的解决方案。我们现在正在使用selenium网格。在看不到它的情况下,听起来好像有什么东西与您试图单击的对象重叠(尽管这个假设可能大错特错)。如果无法访问操作
,您将如何移动到元素?是不是驱动程序.findElements(listImageView).get(0).单击()?(我假设.get(0)
为您提供了要点。)谢谢大家的评论。是的,我确实有一个驱动程序。管理。超时执行。。我相信你说的是对的,因为应用程序在移动版本中有几个移动部分,所以有些东西是重叠的。有没有办法代替添加线程。sleep()?我会试试这个,因为它似乎很有意义!非常感谢。