Selenium:当某些xpath没有元素时如何写条件

时间:2017-05-25 21:31:41

标签: java selenium automation

以下是代码和问题:

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

在我们搜索亚马逊上的任何产品后,我们还可以看到"照明交易"某些产品的选项。现在我在这里试图检查照明交易选项是否存在。如果它存在,那么条件是真的,一切都很好。但是如果没有光照协议,则会出现错误NoSuchElementException,因为div[4]/span/a/i不存在。只有当有一些照明协议时,div[4]/span/a/i才会生效。

如果条件允许,请建议我如何写这个。

完整代码:

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

4 个答案:

答案 0 :(得分:0)

这是你需要做的事情

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

希望这有帮助

答案 1 :(得分:0)

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

 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 :(得分:0)

您需要在Try catch块

中包含if语句

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

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

参考下面的代码:

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

答案 3 :(得分:0)

这是我最喜欢的黑客之一。基本上问题是,在给定页面中检查元素是否可用。 findByElements 方法现在就开始行动了。以下是代码。

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

if块中的语句只会执行,否则不存在定义的元素。希望这会对你有所帮助。

感谢。