2016-04-28 14 views
0

私はwebelementsのリストを持っています。このリストをforeachループで繰り返し、webelementをクリックするとループし、1つの画面から2番目の画面に切り替わります。私の仕事私はボタンを押して、再びwebelementsのリストを持っている最初の画面に戻ります。この全体のシナリオは、最初の反復のためにだけ働くが、2回目の反復では、このような例外を取得し、スイッチングウィンドウがセレニウムwebelementのJavaで動作しません

「org.openqa.selenium.StaleElementReferenceExceptionされます。要素がキャッシュに見つかりません - それは見えたので、おそらく、ページが変更されました コマンド期間またはタイムアウトまで:50.13秒」

後、私のコードは、ある

try {   /** 
     * In this scenario i am iterating through webelementlist, in first iteration i have clicked first 
     * webelement, then fetching some values from second screen after doing my work returning back 
     * to the first one. 
     */    List<WebElement> elementList = driver.findElements(By.className("classname")); 

     for(WebElement webElement: elementList){ 

      webElement.click(); 

      //fetching some values 
      String str = driver.findElement(By.className("classname")).getText(); 

      System.out.println("Value : "+str); 

      //returning back to the first page 
      driver.findElement(By.xpath(".//*[@id='pane']/div/div[1]/div/button")).click(); 
     }  } catch (Exception e) {    e.printStackTrace();  } 

は長い間それに取り組んでいますが、解決策が得られていません。

答えて

1

ページを終了するかDOMをリフレッシュすると、以前見つかったすべてのWebElementsがドライバによって失われます。あなたは、リストを再配置する必要があり、各反復

int size = 1; 
for (int i = 0 ; i < size ; ++i) { 
    List<WebElement> elementList = driver.findElements(By.className("classname")); 
    elementList.get(i).click(); // click the element by index 
    size = elementList.size(); // change "size" to the list size 

    //fetching some values 
    String str = driver.findElement(By.className("classname")).getText(); 

    System.out.println("Value : "+str); 

    //returning back to the first page 
    driver.findElement(By.xpath(".//[@id='pane']/div/div[1]/div/button")).click(); 
} 
1
"org.openqa.selenium.StaleElementReferenceException can occur 
any time, it should be handled and the execution should continue... 

    int count=0; 
    while(count<4) 
    { 
    try{ 
    int size = 1; 
    for (int i = 0 ; i < size ; ++i) { 
     List<WebElement> elementList = driver.findElements(By.className("classname")); 
     elementList.get(i).click(); // click the element by index 
     size = elementList.size(); // change "size" to the list size 

     //fetching some values 
     String str = driver.findElement(By.className("classname")).getText(); 

     System.out.println("Value : "+str); 

     //returning back to the first page 
     driver.findElement(By.xpath(".//[@id='pane']/div/div[1]/div/button")).click(); 
    } } count=count+4; 
    catch(StaleElementReferenceException e) 
    { 
    System.out.println("recover from exception"); 
    count=count+1; continue; 
    } 
} 
関連する問題