2017-02-26 63 views
-1

我已經分配了一個導航到Google的任務,執行搜索並獲得第一個結果頁面中所有結果的標題。在循環瀏覽通過findElements找到的WebElements時跳過了列表索引

我用findElements與xpath和試圖循環通過每個結果和driver.navigate().back()。它拋出了StaleElementReferenceException,我寫了一個try-catch塊並開始初始化catch塊中的findElements

雖然試圖獲得我的findElements的大小(),它被確定爲9時,我用了一個隱含的等待。使用一個明確的,並且Thread.sleep和size()確實設置爲13 - 這正是我期待的。

當一切進展良好時,我可以看到索引在6到12之後跳過。我無法弄清楚發生了什麼。有人能告訴我哪裏出錯了嗎?謝謝!

下面是我的代碼:

@BeforeClass 
    public void setup() { 
     driver = LoadWebDriver.getWebDriver(); 
    } 

    @Test(priority=0) 
    public void navigateTo() { 
     driver.manage().window().maximize(); 
     driver.get("https://www.google.co.in/"); 
    } 

    @Test(priority=1) 
    public void getTheTitles() throws InterruptedException { 

     String xPath = "//h3/a"; 
     int a = 0; 
     boolean loopThru = true; 
     driver.findElement(By.id("lst-ib")).sendKeys("Java",Keys.ENTER); 
//  WebDriverWait wait = new WebDriverWait(driver, 10); 
//  wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.xpath(xPath))); 
//  Thread.sleep(3000); 
     driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS); 
     List<WebElement> dan = driver.findElements(By.xpath(xPath)); 
     System.out.println("** The fucking size is "+dan.size()+" **"); 
     while(loopThru) { 
      try { 
       dan = driver.findElements(By.xpath(xPath)); 
       for (int i = a; i < dan.size(); i++) { 
        if(i==dan.size()-1){ 
         loopThru=false; 
        } 
        dan.get(a).click(); 
        String theTitle = driver.getTitle(); 
        System.out.println(a+" "+theTitle); 
        driver.navigate().back(); 
       } 

      } 

      catch(Exception e) { 
       a++; 
       dan = driver.findElements(By.xpath(xPath)); 
       loopThru = true; 

      } 
     } 
    } 



    @AfterClass 
    public void tearDown() { 

       driver.close(); 
    } 
+0

看看這個答案可能會幫助你http://stackoverflow.com/questions/41999207/not-able-to-get-links-of-all-the-total-pages/42001030#42001030 – NarendraR

回答

0

正如您所指出的,通過點擊其中一個鏈接,導航在搜索結果頁面遠使得它不可能再後來使用該特定頁面的WebElement引用。因此,當您返回搜索結果頁面時,您必須再次點擊find鏈接才能點擊它們。

這是我會怎麼做,(大概根據您最初的代碼):

driver.get("http://www.google.com"); 

driver.findElement(By.id("lst-ib")).sendKeys("Java" + Keys.ENTER); 

int size = new WebDriverWait(driver, 5).until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.xpath("//h3/a"))).size(); 

System.out.println(size + " links"); 

for (int idx = 0; idx < size; idx++) { 
    List<WebElement> links = new WebDriverWait(driver, 5).until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.xpath("//h3/a"))); 

    WebElement link = links.get(idx); 
    String href = link.getAttribute("href"); 

    link.click(); 

    System.out.println(idx + ": " + href + ": " + driver.getTitle()); 

    driver.navigate().back(); 
} 

這將使您的輸出如下:

12 links 
0: https://java.com/de/download/: Download der kostenlosen Java-Software 
1: https://www.java.com/de/download/faq/java_win64bit.xml: Welche Java-Version soll für eine 64-Bit-Version des Windows-Betriebssystems heruntergeladen werden? 
2: https://www.java.com/de/download/faq/remove_olderversions.xml: Warum sollte ich ältere Java-Versionen aus dem System deinstallieren? 
3: https://www.java.com/de/download/help/download_options.xml: Wie installiere ich Java? 
4: https://www.java.com/de/download/faq/whatis_java.xml: Was ist Java und warum brauche ich Java? 
5: https://www.java.com/de/: java.com: Java + Sie 
6: https://de.wikipedia.org/wiki/Java: Java – Wikipedia 
7: https://de.wikipedia.org/wiki/Java-Technologie: Java-Technologie – Wikipedia 
8: https://de.wikipedia.org/wiki/Java-Laufzeitumgebung: Java-Laufzeitumgebung – Wikipedia 
9: https://www.oracle.com/de/java/: Java Software | Oracle Deutschland 
10: http://stackoverflow.com/questions/tagged/java: Newest 'java' Questions - Stack Overflow 
11: http://www.javacafe.at/: JAVA Premiumcafe - Ein Zeichen gehobener Gastlichkeit 

注意兩件事情AOUB該解決方案:

  1. 使用顯式等待語句而不是隱式等待通常會提供更可預測的結果,因爲您有更好的在什麼究竟您希望瀏覽器等待
  2. 不必重新找到的元素我們每次回到搜索結果頁面控制不理想,但這是爲了避免陳舊元問題最簡單的方法。更優雅的解決方案應該是首先找到所有鏈接,將其存儲在href中,稍後單獨導航到頁面以檢索頁面標題。
  3. 因爲我們必須在每次向後導航後再次找到元素,所以我們還必須應用明確等待所有鏈接再次可見。沒有這個,我們會得到相當隨機的結果。
+0

我做想到鏈接的東西,但StaleReference的東西把我帶到別處。謝謝你。 :) –