最终编辑:
刚刚意识到我已将隐式等待设置为 40 秒,因此当.isEmpty()
完成其工作时,它等待了 40 秒才能转到else
语句。感谢您的所有帮助。
问题是它到达 if 语句,只是坐在那里什么也不做。elementId 确实有效,因为如果我删除 if 测试,这两段代码都可以工作。我尝试过的一种方法:
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 不应用于查找不存在的元素, 使用 {@link #findElements(By(} * 并断言零长度响应 相反。
如果元素不存在 - 你不会通过isDisplayed()
方法得到"false",而是从find()
NoSuchElementException
所以你应该做这样的事情:(findElements
(
if(!driver.findElements(By.id(elementId)).isEmpty()){
//do smth
}
刚刚意识到我已经将隐式等待设置为 40 秒,所以当 .isEmpty(( 完成它的工作时,它等待了 40 秒才能转到 else 语句。感谢您的所有帮助。
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);
}
int x = driver.findElement(By.id(elementId)).getLocation().getX();
if(x==0)
{
driver.findElement(By.id(downloadId)).click();
TimeUnit.SECONDS.sleep(7);
}
else{
CenterOnClick.testFail(elementId , driver);
FailScreenShot.test(caseId, driver, targetNumberCell);
}