Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如果元素在SeleniumJava中存在,则执行A或B_Java_Selenium_If Statement - Fatal编程技术网

如果元素在SeleniumJava中存在,则执行A或B

如果元素在SeleniumJava中存在,则执行A或B,java,selenium,if-statement,Java,Selenium,If Statement,最终编辑:刚刚意识到我已将隐式等待设置为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{ CenterOn

最终编辑:刚刚意识到我已将隐式等待设置为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);

 }
问题是它到达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.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文档:

  • findElement不应用于查找不存在的元素, 使用{@link#findElements(By)}*并断言零长度响应 相反
如果元素不存在-通过
isDisplayed()
方法不会得到“false”,但是
NoSuchElementException
来自
find()

因此,您应该执行以下操作:(
findElements


刚刚意识到我已将隐式等待设置为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);

 }

if(driver.findElements(By.id(elementId)).isEmpty()){//也使用了.size()>0 driver.findElement(By.id(downloaddid)).click();TimeUnit.SECONDS.sleep(7)在第二步中尝试了;
。仍然没有执行任何操作。现在它确实单击了按钮,但如果出现错误消息,则不会执行任何操作。很遗憾,没有执行任何操作。请显示发生此错误的页面。
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);
}
 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);

 }