2017-05-25 46 views
1

下面沒有元素如何在條件寫的代碼和問題:硒:當有一些XPath的

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

回答

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

希望這有助於

0

您需要將if語句放在Try catch塊中

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

這是一個我最喜歡的黑客。基本上問題是關於檢查給定頁面中的元素是否可用。 findByElements方法現在開始行動。以下是代碼。

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

if塊內部的語句將只執行存在的元素否則定義。希望這可以幫助你。

謝謝。