2017-11-23 11 views
0

부실 요소 오류가 발생합니다. 나는 그것을 극복하려고 노력했지만 매번 실패했습니다.내 프로그램에서 오래된 요소 참조 오류를 극복하는 방법

d.get("https://iaeme.com/ijciet/index.asp"); 
java.util.List<WebElement>link = d.findElements(By.className("lik")); 
for (int k=1 ; k<= link.size();k++) { 
    link.get(k).click();// stale element error goes here. 
    Thread.sleep(2000); 
    System.out.println(d.getCurrentUrl()); 
} 

이 문제를 해결할 방법이 있습니까?

+0

당신이 링크로 명명 한이 목록의 크기가, 무엇을 시도해보십시오 문이와

link.get(k).click(); 

교체 ? – DG4

+0

각 링크에 대한 클릭으로 색인 페이지에서 탐색하고 있습니까? – Grasshopper

+0

크기가 다르므로이 URL의 크기는 52 – sara

답변

0

예외 처리 throw 문을 try 블록으로 묶고 staleElementReferenceException을 catch하십시오.

d.get("https://iaeme.com/ijciet/index.asp"); 
java.util.List<WebElement>link = d.findElements(By.className("lik")); 
     for (int k=1 ; k<= link.size();k++) { 
      try{ 
       link.get(k).click();// stale element error goes here. 
      Thread.sleep(2000); 
      System.out.println(d.getCurrentUrl()); 
      }catch(StaleElementReferenceException e){ 
       // find the element again OR handle the exception in your way 
       } 
     } 

다른 접근법은 각 반복에서 요소를 찾는 것입니다. 이렇게하면 페이지 탐색 후 webElement의 신선도가 보장됩니다.

d.findElement(By.xpath("(//*[@class='lik'])["+k+"]")).click(); 
+0

인쇄를 계속합니다 - System.out.println (d.getCurrentUrl()); 반복해서. 어떻게해야할까요? – sara

+0

이것은 일반적인 코드 샘플 일뿐입니다. staleElementReferenceException이 발생하는 명령문으로 범위를 좁혀 야합니다. –

+0

다른 접근 방법이 있습니까 ?? – sara

0

d.get("https://iaeme.com/ijciet/index.asp"); 
java.util.List<WebElement>link = d.findElements(By.className("lik")); 
     for (int k=1 ; k<= link.size();k++) { 
      if(link.get(k).isDisplayed()){ 
       link.get(k).click();// stale element error goes here. 
       Thread.sleep(2000); 
       System.out.println(d.getCurrentUrl()); 
      } 
     }