2011-05-19 82 views
2

我正在嘗試查看1-50的所有頁面。不過,我無法在iselementpresent之間遍歷所有頁面。如何使用Selenium遍歷所有表格頁面?

表頁面:

1 2 3 4 5 6 7 8 9 10 ... 

我試圖通過每一個迭代與

If iselementpresent(nextpagenumber) then 
click on nextPageNumber 
else: 
print "done" 

然而,當iselementpresent()達到12頁,它會說12頁不存在,然後給我一個錯誤。是不是iselementpresent應該幫助我避免出現錯誤?

+1

我假設你的代碼是在點擊失敗。如果條件被執行,那麼可能是當時存在12的鏈接,然後出現頁面加載或導致點擊錯誤的內容。在這種情況下,最好使用isVisible。因爲元素可能存在於源中,但可能隱藏。 – 2011-05-19 07:54:58

+0

您是否嘗試在點擊下一頁碼之前等待頁面加載(也可以完成AJAX)。由於可能出現這種情況,如果您不等待頁面加載,下一個數字鏈接可能在點擊時未呈現。 – 2011-05-19 15:20:30

+1

你是對的,我的代碼失敗了,因爲頁面從更改爲後,它轉到下一組頁面。感謝您的幫助! – tang 2011-05-24 04:50:30

回答

0

的基本思路是等待所有的AJAX返回到切換另一個表頁的頁面。 嘗試使用一對方法:流利等待+ isElementDisplayed。

public WebElement fluentWait(final By locator){ 
     Wait<WebDriver> wait = new FluentWait<WebDriver>(driver) 
       .withTimeout(30, TimeUnit.SECONDS) 
       .pollingEvery(5, TimeUnit.SECONDS) 
       .ignoring(NoSuchElementException.class); 

     WebElement foo = wait.until(
new Function<WebDriver, WebElement>() { 
      public WebElement apply(WebDriver driver) { 
         return driver.findElement(locator); 
       } 
       } 
); 
          return foo;    }  ; 

選擇每個表頁面上存在的任何Web元素,並用流利的等待等待它,然後切換到下一個表頁面。 +添加額外的檢查與

String cssSelector= .... 
driver.findElement(By.cssSelector(cssSelector)).isDisplayed() 

希望這對你的作品)