2016-01-17 53 views
0

主要頁面包含超過300個鏈接,點擊主頁上的每一個環節上表中打開新窗口(當然)。我總是需要來自同一個位置的表值,但是......有時(但有時候)(需要)表值也是一個鏈接,點擊它後會打開新窗口。 如果點擊該表值打開新窗口(使用新表),我需要從該新窗口的特定表值,如果不是(如果原始表值不是鏈接)我只需要原始表值。
我試圖用下面的代碼但出錯...偶爾鏈接的處理

異常在線程「主」 org.openqa.selenium.NoSuchElementException:無法找到元素:{「方法」:「的xpath」,「選擇器」: 「.//*[@id='aodds-info']/div[2]/table/tbody/tr[3]/td[2]」} 命令持續時間或超時:33毫秒

package newpackage; 

    import java.io.File; 
    import java.io.FileInputStream; 
    import java.io.FileNotFoundException; 
    import java.io.FileOutputStream; 
    import java.io.IOException; 
    import java.util.Iterator; 
    import java.util.List; 
    import java.util.NoSuchElementException; 
    import java.util.Set; 

    import org.openqa.selenium.By; 
    import org.openqa.selenium.WebDriver; 
    import org.openqa.selenium.WebElement; 
    import org.openqa.selenium.firefox.FirefoxDriver; 

    public class newdist { 

    public static void main(String[] args) throws IOException, InterruptedException { 

    // Open main page 

     WebDriver driver = new FirefoxDriver(); 
     driver.get("Main page link"); 
     Thread.sleep(5000); 

     // Maximize main page window   
      driver.manage().window().maximize(); 
     Thread.sleep(1000); 

      // List off all links on Main page 
      List<WebElement> lista1 = driver.findElements(By.cssSelector(".first-cell.tl>a")); 

     // loop trough all links on Main page 

     for(int j=0;j<lista1.size();j++){ 

     WebElement link = lista1.get(j); 
     List<WebElement> links = driver.findElements(By.cssSelector(".first-cell.tl>a")); 
     String homePage = driver.getWindowHandle(); 
     link.click(); 
     Thread.sleep(3000); 

     // Window handles block 

       Set<String>windows=driver.getWindowHandles(); 
     Iterator iterator = windows.iterator(); 
     String currentWindowId; 
     while (iterator.hasNext()){ 
      currentWindowId = iterator.next().toString(); 
      if(! currentWindowId.equals(homePage)){ 
       driver.switchTo().window(currentWindowId); 
       Thread.sleep(3000); 

     // "clicking" on specific table value (clicking maybe opens new window)  

      driver.findElement(By.xpath(".//*[@id='sortable-1']/tbody/tr[6]/td[1]/span")).click(); 
      Thread.sleep(3000); 

      // if clicking opens new window print specific value from table in that new window 

       try { 
      String s0 = driver.findElement(By.xpath(".//*[@id='aodds-info']/div[2]/table/tbody/tr[3]/td[2]")).getText(); 
      System.out.println(s0); 
        } 

     // if clicking doesn't open new window print current table value from current window  

      catch (NoSuchElementException e){ 
      String s0 = driver.findElement(By.xpath(".//*[@id='sortable-1']/tbody/tr[6]/td[1]/span")).getText(); 
      System.out.println(s0); 


     } 

      // return to Main page 
     finally{ 

      driver.close(); 
      driver.switchTo().window(homePage); 
      Thread.sleep(2000); 
      } 
      } 
    } 

} 
} 
} 
+0

忘記,其實它工作正常,但是當表中的值(從主網頁後第一個窗口)不是一個鏈接那麼Java返回錯誤... – Pavle

+0

你能與錯誤添加表的HTML? – Guy

+0

採集表值實際上不是不亞於這是一個問題「是需要的或新的表值鏈接新窗口沒有鏈接收集現有值」 ... – Pavle

回答

0

NoSuchElementExceptionjava.util進口。要捕捉網絡元素例外,您需要從org.openqa.selenium導入。

作爲一個側面說明,使用explicit and implicit wait是更好的做法,然後使用Thread.sleep

+0

是的,就是這樣!非常感謝!現在看起來很愚蠢的錯誤,但事實是你救了我的一天......)再次感謝。 – Pavle

+0

@Pavle歡迎:) – Guy