【问题标题】:Selenium: how to write if condition when there is no element for some xpathSelenium:当某些xpath没有元素时如何编写if条件
【发布时间】:2017-05-25 21:31:41
【问题描述】:

下面是代码和问题:

if(result.findElement(By.xpath(".//*[@class='s-item-container']/div[4]/span/a/i")).getText().equals("Lightning Deal"))

我们在亚马逊上搜索任何产品后,我们还可以在某些产品上看到“Lighting Deal”选项。现在,我在这里尝试检查是否存在照明交易选项。如果它存在,则条件为真,一切正常。但是如果没有照明交易,那么这会给出错误NoSuchElementException,因为那时div[4]/span/a/i 不存在。这个div[4]/span/a/i 只有在有一些照明交易时才会出现。

请建议我如何写这个 if 条件。

完整代码:

//All the products after searching in List
List<WebElement> resultsList = driver.findElements(By.xpath(".//*[starts-with(@id, 'result_')]"));

for(WebElement result:resultsList)
{
System.out.println("Name of the product:"+result.findElement(By.tagName("h2")).getText());

System.out.println("Brand: "+result.findElement(By.xpath(".//*[@class='s-item-container']/div[3]/div[2]/span[2]")).getText());

    if(result.findElement(By.xpath(".//*[@class='s-item-container']/div[4]/span/a/i")).getText().equals("Lightning Deal"))
    {
        String sp =result.findElement(By.xpath(".//*[@class='s-item-container']/div[5]/div[1]/a/span[2]")).getText();
        System.out.println("Selling Price: "+sp);       
    }

    else
    {
        System.out.println("Selling Price:"+result.findElement(By.xpath(".//*[@class='s-item-container']/div[4]/div[1]/a/span[2]")).getText());
    }
}

【问题讨论】:

    标签: java selenium automation


    【解决方案1】:

    您可以在获取文本之前检查该元素是否存在于页面上

     for(WebElement result:resultsList)
     {
     System.out.println("Name of the 
     product:"+result.findElement(By.tagName("h2")).getText());
    
     System.out.println("Brand: "+result.findElement(By.xpath(".//*[@class='s-
     item-container']/div[3]/div[2]/span[2]")).getText());
    
    if(result.findElements(By.xpath(".//*[@class='s-item-
    container']/div[4]/span/a/i")).size() != 0
    && result.findElement(By.xpath(".//*[@class='s-item-
    container']/div[4]/span/a/i")).getText().equals("Lightning Deal"))
    {
      String sp =result.findElement(By.xpath(".//*[@class='s-item-
     container']/div[5]/div[1]/a/span[2]")).getText();
      System.out.println("Selling Price: "+sp);       
    } 
    else
    {
        System.out.println("Selling Price:"+result.findElement(By.xpath(".//*
    [@class='s-item-container']/div[4]/div[1]/a/span[2]")).getText());
    } 
    }
    

    【讨论】:

      【解决方案2】:

      这是你需要做的

      List<WebElement> deals = result.findElements(By.xpath(".//*[@class='s-item-container']/div[4]/span/a/i"));
      if(deals.size() > 0)
      {
          if(deals.get(0).getText().equals("Lightning Deal"))
          {
              String sp =result.findElement(By.xpath(".//*[@class='s-item-container']/div[5]/div[1]/a/span[2]")).getText();
              System.out.println("Selling Price: "+sp);       
          }
          else
          {
              System.out.println("Selling Price:"+result.findElement(By.xpath(".//*[@class='s-item-container']/div[4]/div[1]/a/span[2]")).getText());
          }
      }
      

      希望对你有帮助

      【讨论】:

        【解决方案3】:

        你需要将 if 语句包含在 Try catch 块中

        将您的 if 条件括在 try 块中。因此,如果您的元素在那里,那么您将能够在if 条件下执行您的操作。

        如果没有这样的 xpath,那么它将抛出异常,该异常将在 catch 块下处理,所以只需在 catch 块下写 else 部分

        参考以下代码:

        //All the products after searching in List
        List<WebElement> resultsList = driver.findElements(By.xpath(".//*[starts-with(@id, 'result_')]"));
        
        for(WebElement result:resultsList)
        {
            System.out.println("Name of the product:"+result.findElement(By.tagName("h2")).getText());
        
            System.out.println("Brand: "+result.findElement(By.xpath(".//*[@class='s-item-container']/div[3]/div[2]/span[2]")).getText());
        
            try
            {
                    if(result.findElement(By.xpath(".//*[@class='s-item-container']/div[4]/span/a/i")).getText().equals("Lightning Deal"))
                    {
                        String sp =result.findElement(By.xpath(".//*[@class='s-item-container']/div[5]/div[1]/a/span[2]")).getText();
                        System.out.println("Selling Price: "+sp);       
                    }
            }
            catch(Exception e)
        
            {
                    System.out.println("Selling Price:"+result.findElement(By.xpath(".//*[@class='s-item-container']/div[4]/div[1]/a/span[2]")).getText());
            }
        }
        

        【讨论】:

          【解决方案4】:

          这是我最喜欢的 hack 之一。基本上问题是关于检查给定页面中的元素可用性。 findByElements 方法现在开始起作用。下面是代码。

          if(driver.findElements(ByXpath("XPATH_HERE")).size>0)
          {
            //DO THE STUFF YOU NEED TO DO IF THE ELEMENT EXISTS
          }
          

          if 块中的语句将仅在存在定义的元素时执行,否则将不执行。希望对您有所帮助。

          谢谢。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2020-10-09
            • 2021-08-05
            • 1970-01-01
            • 2018-03-21
            相关资源
            最近更新 更多