【问题标题】:How to use if else condition for searching element using WebDriver in Selenium?如何使用 if else 条件在 Selenium 中使用 WebDriver 搜索元素?
【发布时间】:2014-01-15 08:37:32
【问题描述】:
  1. 我用 else if 概念
  2. 我没有使用 try catch finally 概念 webdriver 元素查找使用 elseif 循环

我希望它如何工作:

  1. 首先它会检查“if”条件是[if(driver.findElement(By.xpath("username")).isDisplayed())],如果没有找到它就不会打印任何语句。
  2. 因为没有看到“if”条件,它将进入“elseif”条件,即[else if(driver.findElement(By.id("username")).isDisplayed())],并且当“else if”语句为真时,它将打印并执行循环中的任何操作..
    让我知道上面的statemnts我下面的代码是否有效......

.

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");   
    }    
}  

问题:它正在检查 if 条件,并且由于在该条件下未找到元素,因此它退出循环不会进入 elseif...

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

 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);
     }
 }

程序的输出是:Grade = B

【问题讨论】:

    标签: java selenium xpath selenium-webdriver


    【解决方案1】:

    当然它会脱离循环。 driver.findElement 如果找不到元素,则抛出 NoSuchElementException。您的 xpath 不好,因此它不会找到导致您的异常的元素。

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

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

    【讨论】:

    • 使用 Paul 建议的逻辑或使用 try/catch/finally 来捕获 NoSuchElementException 并执行您想要的任何逻辑。
    【解决方案2】:

    你可以试试这个。 如果显示元素,则单击元素,否则脚本继续。

    List<WebElement> dynamicElement = driver.findElements(By.id("element"));
    
    if (dynamicElement.size() != 0) {
        System.out.println("Element present");
        driver.findElement(By.id("element")).click();
    } else {
        System.out.println("Element not present");
    }
    

    【讨论】:

      【解决方案3】:

      示例:在我的脚本中看起来像 step1,step2,step3,complete。 有时这个步骤改变了 step1,step2,step3,step4,complete 。这些步骤有相同的“XPATH”。所以我不知道如何给出“LOOP”条件。我附上了我的代码..请给出好的建议

             for(int i=0; i<=10; i++) {
              driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS) ;
                  JavascriptExecutor js1=(JavascriptExecutor)driver;
                  js1.executeScript("window.scrollTo(0,document.body.scrollHeight)");
                  
                 WebElement clickYESButton2 = driver. findElement(By.xpath("/html/body/div[1]/main/ui-view/ui-view/section/div[2]/div[2]/div/form/div/div[2]/div[1]/div/div/label[1]/span"));
                 clickYESButton2.click();
                 driver.manage().timeouts().implicitlyWait(100,TimeUnit.SECONDS) ;
                 System.out.println(driver.findElement(By.xpath("//*[@id=\"main-container\"]/ui-view/ui-view/section/div[2]/div[2]/div/form/div/div[1]/h5/span")).getText());
                 WebElement clickNEXTButton2 = driver. findElement(By.xpath("//div[@class='card']//button[contains(text(),'Next Step')][1]"));
                 clickNEXTButton2.click();
                Thread.sleep(5000);
                 if(driver.findElement(By.xpath("//*[contains(text(),'Complete Review')]")).isDisplayed()){     
                     driver.findElement(By.xpath("//*[contains(text(),'Complete Review')]")).click();  
                   }
                 else {
                         reviewPgApplicationPage.clickYESButton.click();
                       
                      
                                              }
              
              
      

      【讨论】:

        猜你喜欢
        • 2019-04-25
        • 2020-08-30
        • 2014-12-15
        • 2022-11-17
        • 2018-09-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-01-02
        相关资源
        最近更新 更多