作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
最终编辑: 刚刚意识到我已将隐式等待设置为 40 秒,因此当 .isEmpty()
完成其工作时,它等待了 40 秒到 else
语句。感谢您的帮助。
问题是它到达 if 语句后只是坐在那里不执行任何操作。如果我删除 if 测试,则 elementId 确实可以像两段代码一样工作。我尝试过的一种方法:
if(!driver.findElement(By.id(elementId )).isDisplayed()){
driver.findElement(By.id(downloadId)).click();
TimeUnit.SECONDS.sleep(7);
}else{
CenterOnClick.testFail(elementId , driver);
FailScreenShot.test(caseId, driver, targetNumberCell);
}
另一种方式:(最终编辑:这个可以解决这个问题。)
if(driver.findElements(By.id(elementId)).isEmpty()){ //also used .size() > 0
driver.findElement(By.id(downloadId)).click();
TimeUnit.SECONDS.sleep(7);
}else{
CenterOnClick.testFail(elementId , driver);
FailScreenShot.test(caseId, driver, targetNumberCell);
}
我也尝试过改变一些事情。仅当您在一个月内下载该文件的次数不超过两次时,我尝试单击的按钮才可用。之后会出现一条错误消息。奇怪的是,如果我将代码更改为:
if(driver.findElement(By.id(elementId )).isDisplayed()){
CenterOnClick.testFail(elementId , driver);
FailScreenShot.test(caseId, driver, targetNumberCell);
}else{
driver.findElement(By.id(downloadId)).click();
TimeUnit.SECONDS.sleep(7);
}
它有效,但仅在显示错误消息时有效。如果按钮在那里,它仍然会坐在那里。如果我能提供任何帮助,我将不胜感激,因为我已经在这方面花费了超过 16 个小时。谢谢。
编辑:我开始认为我的问题与网站有关,而不是与我的代码有关。
最佳答案
请阅读 WebElement 文档:https://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/WebElement.html#findElement-org.openqa.selenium.By-
- findElement should not be used to look for non-present elements, use {@link #findElements(By)} * and assert zero length response instead.
如果元素不存在 - 您不会通过 isDisplayed()
方法得到“false”,但会从 find()
得到 NoSuchElementException
>
所以你应该这样做:(findElements
)
if(!driver.findElements(By.id(elementId)).isEmpty()){
//do smth
}
关于java - 如果元素存在,则在selenium Java中执行A,否则执行B,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44847158/
我是一名优秀的程序员,十分优秀!