2014-06-18 86 views
0

我試圖獲取剖面類中存在的所有圖像的屬性。我試圖獲得的圖像屬性是:獲取剖面類中所有圖像的圖像屬性

  1. 圖像高度。

  2. 圖像寬度

  3. 圖片src

我試圖用硒的webdriver實現。

我是Selenium的新手,所以我瀏覽了網絡教程和互聯網上提供的各種答案。

我的代碼是這樣的:

public void findAllImages(){ 
driver=new FirefoxDriver(); 
driver.manage().window().maximize(); 
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS); 

baseUrl="http://northeastindiaholidays.com/"; 
driver.get(baseUrl); 


WebElement menu=driver.findElement(By.className("destinations clearfix full")); 
List<WebElement> allImages=driver.findElements(By.tagName("img")); 


List<String> widthofImage = new ArrayList<String>(); 
List<String> heightofImage = new ArrayList<String>(); 
List<String> srcofImage = new ArrayList<String>(); 

for (WebElement imageFromList: allImages){ 
    widthofImage.add(imageFromList.getAttribute("width")); 
     heightofImage.add(imageFromList.getAttribute("height")); 
     srcofImage.add(imageFromList.getAttribute("src")); 

     System.out.println(widthofImage); 
     System.out.println(heightofImage); 
     System.out.println(srcofImage); 

    } 

} 
} 

HTML頁面看起來像這樣:

<section class="destinations clearfix full" style="margin-bottom:10px!important"> 
    <h1>Beach Destinations</h1> 
    <article class="location_item one-fourth fluid-item"> 
    <figure> 
    <a title="" href="http://northeastindiaholidays.com/?location=goa"> 
    <img width="270" height="152" alt="" src="http://northeastindiaholidays.com/wp- content/uploads/2014/06/Goa.gif"/> 
    </a> 
    </figure> 
    <div class="details"> 
    </article> 

頁面的網址是:North East India Holidays

+0

那麼你有問題嗎? – SiKing

+0

是的,我意識到我完全錯過了描述問題。我在使用目標部分類選擇類名時出錯。 以下是我收到的錯誤圖片的鏈接 [link] https://drive.google.com/file/d/0B3aMk3yP4IBFNW8tV0FqRXRPWnc/edit?usp=sharing – demouser123

+0

請編輯原始帖子並附上新信息。另外:你是否真的截下了一個文本窗口?爲什麼? – SiKing

回答

1

試試下面的代碼:

import java.io.IOException; 
import java.util.ArrayList; 
import java.util.List; 
import org.openqa.selenium.By; 
import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.WebElement; 
import org.openqa.selenium.firefox.FirefoxDriver; 
import org.testng.annotations.BeforeClass; 
import org.testng.annotations.Test; 
import com.thoughtworks.selenium.webdriven.commands.GetText; 

public class Sample 
{  
WebDriver driver; 
@BeforeClass 
public void BeforeClass() throws InterruptedException, IOException 
{ 
    System.setProperty("webdriver.firefox.profile", "default"); 
    driver = new FirefoxDriver(); 
    driver.manage().window().maximize(); 
    driver.get("http://northeastindiaholidays.com/"); 
    Thread.sleep(4000); 

} 
@Test 
public void fetchCustKeys() throws Exception 
{ 
    //WebElement menu1=driver.findElement(By.xpath("//*[@id='content']/section[1]")); 
    WebElement menu=driver.findElement(By.xpath("//*[@id='content']/section[./h1[text()='Beach Destinations']]")); 
    List<WebElement> allImages=menu.findElements(By.tagName("img")); 

    List<String> widthofImage = new ArrayList<String>(); 
    List<String> heightofImage = new ArrayList<String>(); 
    List<String> srcofImage = new ArrayList<String>(); 
    for (WebElement imageFromList: allImages) 
    { 
     widthofImage.add(imageFromList.getAttribute("width")); 
     heightofImage.add(imageFromList.getAttribute("height")); 
     srcofImage.add(imageFromList.getAttribute("src")); 
    } 
    System.out.println(widthofImage); 
    System.out.println(heightofImage); 
    System.out.println(srcofImage); 
} 
} 

除了該部分的XPATH以外,代碼中還有兩處更正。

1)通過使用上面提到的XPATH作爲節,您可以通過改變節的名稱來重新使用XPATH。 如:

WebElement menu=driver.findElement(By.xpath("//*[@id='content']/section[./h1[text()='Beach Destinations']]")); 
WebElement menu=driver.findElement(By.xpath("//*[@id='content']/section[./h1[text()='Top destinations around North East India']]")); 

2)通過使用下面的語句,你會得到整個網頁圖像。

List<WebElement> allImages=driver.findElements(By.tagName("img")); 

所以我們需要改變的聲明類似下面讓特定部分的圖像:

List<WebElement> allImages=menu.findElements(By.tagName("img")); 

3)移動的ArrayList S上打印出for循環

的輸出:

[TestNG] Running: 
    C:\Users\HEMA\AppData\Local\Temp\testng-eclipse-717955280\testng-customsuite.xml 

[268, 268, 268, 268] 
[152, 152, 152, 152] 
[http://northeastindiaholidays.com/wp-content/uploads/2014/06/Goa.gif, http://northeastindiaholidays.com/wp-content/uploads/2014/06/Puducherry_beach.jpg, http://northeastindiaholidays.com/wp-content/uploads/2014/06/Kerla.jpg, http://northeastindiaholidays.com/wp-content/uploads/2014/06/Leh.jpg] 
PASSED: fetchCustKeys 

=============================================== 
    Default test 
    Tests run: 1, Failures: 0, Skips: 0 
=============================================== 


=============================================== 
Default suite 
Total tests run: 1, Failures: 0, Skips: 0 
=============================================== 

[TestNG] Time taken by [email protected]: 469 ms 
[TestNG] Time taken by [FailedReporter passed=0 failed=0 skipped=0]: 1 ms 
[TestNG] Time taken by [email protected]: 303 ms 
[TestNG] Time taken by [email protected]: 94 ms 
[TestNG] Time taken by [email protected]: 154 ms 
[TestNG] Time taken by [email protected]: 11 ms 
+0

嗨Hemasundar,你提到的代碼是做同樣的事情 - 它打印頁面中存在的所有圖像及其屬性。 – demouser123

+0

@geeko_zac我已經添加了輸出,我越來越。你怎麼會得到錯誤的輸出。正確複製代碼並重試。 – HemaSundar

+0

是的,它的工作。其實我的eclipse沒有運行這個程序。非常感謝。你再次來救我。接受這個答案。乾杯 – demouser123