How do you resolve STALE ELEMENT EXCEPTION?

STALE ELEMENT Exception occurs when Webdriver is trying to find a WebElement that is present on the page but it cannot be referenced.

Suppose that user lands on the Homepage and there is "Courses" button on the Home page of your application. The user identifies the button as follows.

Therefore, you write the code as:

driver.get("www.testweb.com/homepage");

WebElement courseBtn = driver.findElement(By.xpath("//div/btn[text(),'Courses']"));

Now the user goes to another page say "Tutors" and comes back again to Home page and tries to click on Courses link.

Now when you use courseBtn.click(); ---> Throws STALE ELEMENT Exception.

SOLUTION:

To Handle this problem we will have to identify the button again.
i.e again write following code:

WebElement courseBtn2 = driver.findElement(By.xpath("//div/btn[text(),'Courses']"));
courseBtn2.click(); // This will now work


Comments

Popular posts from this blog

Jenkins CICD in One Page

Why do we need a build tool?

Deutsche Bank Interview Questions - 2024