How To Handle staleelementreferenceexception Selenium Webdriver

staleelementreferenceexception

There are many unlike exceptions In selenium webdriver software testing tool. I bring described five of them on THIS PAGE which nosotros are facing frequently. One to a greater extent than webdriver exception Is stale chemical component reference exception. I recollect unopen to of y'all bring faced or volition confront this exception when y'all travel selenium webdriver evidence for your software web application. Just latterly I bring faced this exception In my evidence amongst exception message every bit bellow.


Exception : 
org.openqa.selenium.StaleElementReferenceException: Element non flora inward the cache - mayhap the page has changed since it was looked up
Command duration or timeout: 10.03 seconds

What Is StaleElementReferenceException?
Stale way former or nosotros tin privy nation no longer fresh element. Let me depict y'all In really uncomplicated words. Example : You bring a search text box on software spider web page. When y'all search for unopen to keyword, text box's seat teach changed on page. So In this case, Look together with feel, Identifiers etc. of text box volition rest same but what Internally happened Is -> JS library has deleted previous text box together with replaced It amongst novel same text box. So forthwith If y'all volition teach to work same text box using previously located reference In your software test, You volition get StaleElementReferenceException In console.

If y'all volition travel bellow given example, It volition throw StaleElementReferenceException.
package Testing_Pack;  import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.Keys; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; import org.testng.annotations.BeforeTest; import org.testng.annotations.Test;  populace shape StaleElement {  WebDriver driver;  @BeforeTest  populace void setup() throws Exception {   driver =new FirefoxDriver();        driver.manage().window().maximize();   driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);   driver.get("http://www.github.com");  }    @Test  populace void getExe() throws InterruptedException{     //Located chemical component together with stored It's reference In variable.   WebElement Search_Box = driver.findElement(By.xpath("//input[@name='q']"));   //Used chemical component reference variable to locate chemical component together with perform search.   Search_Box.sendKeys("Hello");   Search_Box.sendKeys(Keys.ENTER);   Thread.sleep(5000);      //After search operation, Element's seat Is changed.   //Now I am using same reference variable to clear search text box.   //So here, WebDriver volition last non able to locate chemical component using same reference together with It volition throw StaleElementReferenceException.   Search_Box.clear();    } }

This Is but example. You tin privy bring unlike province of affairs or scenario. This volition come about solely unopen to times but y'all must know how to resolve It.There may last another reasons also for this exception. Now permit us endeavour to resolve or handgrip this exception.

So Exception Is generating on Search_Box.clear(); business because software spider web application page has been refreshed together with search text box's seat Is changed during previous search operation. In unopen to cases, After refreshing the page or javascript action, Position of chemical component volition remains same but all the same y'all volition teach this exception.

I bring 2 possible solutions to handgrip this situation.

Proposed Solution 1 :
Create a purpose together with perform text box clear functioning Inside It every bit shown In bellow illustration using piece loop. Replace @Test method of inward a higher house illustration amongst bellow given.
@Test  populace void getExe() throws InterruptedException {   // Located chemical component together with stored It's reference In variable.   WebElement Search_Box = driver.findElement(By.xpath("//input[@name='q']"));   // Used chemical component reference variable to locate chemical component together with perform search.   Search_Box.sendKeys("Hello");   Search_Box.sendKeys(Keys.ENTER);   Thread.sleep(5000);    // After search operation, Element's seat Is changed.   //Call purpose amongst chemical component get upwardly to perform clear operation.   handleStaleElement("q");  }   // This purpose volition handgrip stalelement reference exception  populace void handleStaleElement(String elementName) {   int count = 0;   //It volition endeavour four times to notice same chemical component using name.   piece (count < 4) {    endeavour {     //If exception generated that way It Is non able to notice chemical component thence select receive got of block volition handgrip It.     WebElement staledElement = driver.findElement(By.name(elementName));     //If exception non generated that way chemical component flora together with chemical component text teach cleared.     staledElement.clear();        } select receive got of (StaleElementReferenceException e) {     e.toString();     System.out.println("Trying to recover from a stale chemical component :" + e.getMessage());     count = count + 1;    }    count = count + 4;   }  }




Proposed Solution 2 :
You tin privy create same matter using for loop Inside @Test method every bit bellow.
@Test  populace void getExe() throws InterruptedException {   // Located chemical component together with stored It's reference In variable.   WebElement Search_Box = driver.findElement(By.xpath("//input[@name='q']"));   // Used chemical component reference variable to locate chemical component together with perform search.   Search_Box.sendKeys("Hello");   Search_Box.sendKeys(Keys.ENTER);   Thread.sleep(5000);    // After search operation, Element's seat Is changed.   //use for loop.   for(int i=0; i<4;i++)             endeavour {              driver.findElement(By.xpath("//input[@name='q']")).clear();                 break;             } catch(StaleElementReferenceException e) {               e.toString();         System.out.println("Trying to recover from a stale chemical component :" + e.getMessage());                    }  }

If anyone of y'all bring faced same job of staleelementreferenceexception and resolved It In whatsoever other way thence y'all tin privy ship service your solution past times commenting bellow thence others tin privy work It.
More interesting articles here :Generation Enggelmundus Internet Marketing Tool here :Zeageat IM http://www.software-testing-tutorials-automation.com/
Post a Comment (0)
Previous Post Next Post