2015-01-08 41 views
0

我想在亞馬遜搜索時獲得結果數。以下是該方案如何使用硒獲得總搜索結果數量

打開一個新的瀏覽器實例 導航到amazon.in 在文本框中 單擊搜索按鈕 驗證的Web元素顯示搜索結果的數量對我們的期望值搜索查詢

public void searchTestOne(){ 

    WebDriver driver = new FirefoxDriver(); 
    driver.get("http://www.amazon.in"); 
    driver.manage().window().maximize();   

    driver.findElement(By.id("twotabsearchtextbox")).sendKeys("Books"); 
    driver.findElement(By.className("nav-submit-input")).click(); 

    int result = driver.findElements(By.xpath(".//*[@id='atfResults']/ul[@id='s-results-list-atf']/li")).size(); 
    System.out.println(result); 

    driver.close(); 

    driver.quit(); 

    } 

從上面的代碼顯示只有像「16」裏因爲有這麼多頁,總成績爲2000 +第一頁結果計數。

任何人都可以請在這個建議。

回答

3

用於獲取總搜索結果數的Webelment不正確。正確的元素/標籤爲 「H2」 ID爲 「S-結果數」

以下行應該給你的搜索結果數數:

String result = driver.findElement(By.id("s-result-count")).getText().split(" ")[2]; 
1

試試這個:

result = driver.findElements(By.xpath(".//*[@id='atfResults']/ul[@id='s-results-list-atf']/li")).count(); 
3

看起來你正試圖通過計算書籍數量來獲取書籍數量。這樣,您將不得不導航到每個頁面,並相應地增加您的計數。相反,你可以得到結果的總數字,或者,如果你看一下頁面左上角如下圖所示的圖像:

enter image description here

所以,你可以得到結果的總數從這個代碼作爲@Surya建議:

String result = driver.findElement(By.id("s-result-count")).getText().split(" ")[2]; 
System.out.println(result); 
相關問題