Java 如何使用 if else 条件在 Selenium 中使用 WebDriver 搜索元素?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21132556/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 06:48:58  来源:igfitidea点击:

How to use if else condition for searching element using WebDriver in Selenium?

javaseleniumxpathselenium-webdriver

提问by user3197241

  1. I used else if concept
  2. I have not used try catch finally concept webdriver element finding using elseif loop
  1. 我使用了 else if 概念
  2. 我没有使用 try catch finally 概念使用 elseif 循环查找 webdriver 元素

how I want it to work :

我希望它如何工作:

  1. First it will check for "if" condition that is [if(driver.findElement(By.xpath("username")).isDisplayed())]and if it is not found it will not print any statement.
  2. because "if" condition is not seen it will go to "elseif" condition that is [else if(driver.findElement(By.id("username")).isDisplayed())]and as "else if" statement is true, it will print and do what ever is there in loop..
    let me know on above statemnts does my below code works or not...
  1. 首先它会检查“if”条件,[if(driver.findElement(By.xpath("username")).isDisplayed())]如果没有找到,它不会打印任何语句。
  2. 因为没有看到“if”条件,它会进入“elseif”条件,[else if(driver.findElement(By.id("username")).isDisplayed())]并且“else if”语句为真,它将打印并执行循环中的任何操作..
    让我知道上面的语句是否在下面代码有效与否...

.

.

public void mytrip()throws Exception{   
    driver.get("http://yahoomail.com/");   
    if(driver.findElement(By.xpath("username")).isDisplayed()){     
        driver.findElement(By.xpath("username")).click();   
        System.out.println("clicked"); 
    } else if(driver.findElement(By.id("username")).isDisplayed()){ 
        driver.findElement(By.id("username")).click(); 
        System.out.println("clicked in else if");   
    }    
}  

problem: it is checking for if condition and as element is not found in that condition it is coming out of loop not going to elseif...

问题:它正在检查 if 条件,并且由于在该条件中未找到元素,它正在退出循环而不是 elseif ...

according to below concept my above code should work i feel.. if not then please let me know how to work with that..

根据下面的概念,我上面的代码应该可以工作,我觉得..如果没有,请告诉我如何使用它..

 class IfElseDemo {
     public static void main(String[] args) {
         int testscore = 76;
         char grade;
         if (testscore > 90) {
             grade = 'A';
         } else if (testscore < 80) {
             grade = 'B';
         } else if (testscore > 60) {
             grade = 'C';
         }
         System.out.println("Grade = " + grade);
     }
 }

The output from the program is: Grade = B

程序的输出是: Grade = B

采纳答案by diazazar

Of course it gets out of the loop. driver.findElementthrows NoSuchElementException if it cannot find the element. Your xpath is not good so it will not find the element resulting in your exception.

当然,它脱离了循环。如果找不到元素,driver.findElement将抛出 NoSuchElementException。你的 xpath 不好,所以它不会找到导致你异常的元素。

Boolean isPresent = driver.findElements(By.yourLocator).size()<0

布尔值 isPresent = driver.findElements(By.yourLocator).size()<0

try something like that to check for elements.

尝试类似的方法来检查元素。