Reputation: 73
Hi I am working with the selenium webdriver and while running the below script, i am facing stale element exception error at the place which i highlighted in the script.
My script stops over there and i can't able to run.
The error message is - Stale Element exception error. The element is not found in the cache perhaps the page has changed since it was looked up.
Anyone please help me in this case and guide me to rectify this error.
Regards, Vignesh K S
@Test
public void testClient() throws Exception {
driver.get("http://t1accounts.govreports.com.au/?service=govreports");
driver.findElement(By.id("Password")).clear();
driver.findElement(By.id("Password")).sendKeys("Viki2607");
driver.findElement(By.id("UserName")).clear();
driver.findElement(By.id("UserName")).sendKeys("[email protected]");
driver.findElement(By.id("btnLogin")).click();
driver.findElement(By.xpath(".//*[@id='side-menu']/li[2]/a/span")).click();
driver.findElement(By.cssSelector("span.hide380")).click();
driver.findElement(By.id("Individual")).click();
//driver.wait(5);
**driver.findElement(By.id("ABN")).clear();**
driver.findElement(By.id("ABN")).sendKeys("38091241128");
driver.findElement(By.id("TFN")).clear();
driver.findElement(By.id("TFN")).sendKeys("100000001");
driver.findElement(By.id("BusinessName")).clear();
driver.findElement(By.id("BusinessName")).sendKeys("LORGE CONSULTING (AUSTRALIA) PTY LTD");
driver.findElement(By.id("TradingName")).clear();
driver.findElement(By.id("TradingName")).sendKeys("LORGE CONSULTING (AUSTRALIA) PTY LTD");
//driver.findElement(By.xpath("//ul[@id='Individual_Salutation_listbox']/li")).click();
driver.findElement(By.id("Individual_Salutation")).clear();
driver.findElement(By.id("Individual_Salutation")).sendKeys("Ms");
//new Select(driver.findElement(By.id("Individual_Salutation"))).selectByVisibleText("Mr");
//driver.findElement(By.id("Individual_Salutation")).sendKeys("Mr");
driver.findElement(By.id("Individual_FirstName")).clear();
driver.findElement(By.id("Individual_FirstName")).sendKeys("Joan");
driver.findElement(By.id("Individual_LastName")).clear();
driver.findElement(By.id("Individual_LastName")).sendKeys("Ignatius");
driver.findElement(By.id("Individual_Phone")).clear();
driver.findElement(By.id("Individual_Phone")).sendKeys("042323155");
driver.findElement(By.id("Individual_Email")).clear();
driver.findElement(By.id("Individual_Email")).sendKeys("[email protected]");
driver.findElement(By.id("PostalAddress_Line1")).clear();
driver.findElement(By.id("PostalAddress_Line1")).sendKeys("Walker Street");
driver.findElement(By.id("PostalAddress_City")).sendKeys("SYDNEY");
driver.findElement(By.id("PostalAddress_Region")).sendKeys("NSW");
driver.findElement(By.id("PostalAddress_Postcode")).sendKeys("1001");
Thread.sleep(5000);
driver.findElement(By.id("PostalAddress_Country")).sendKeys("Australia");
//Select objSelect = new Select(null);
//new Select(driver.findElement(By.id("PostalAddress_City"))).selectByVisibleText("SYDNEY, NSW, Australia, 1001");
driver.findElement(By.id("saveClient")).click();
Thread.sleep(5000);
}
Upvotes: 1
Views: 523
Reputation: 21
When a Stale Element Exception occurs!!
Stale element exception can happen when the libraries supporting those textboxes/ buttons/ links has changed which means the elements are same but the reference has now changed in the website without affecting the locators. Thus the reference which we stored in our cache including the library reference has now become old or stale because the page has been refreshed with updated libraries.
for(int j=0; j<5;j++)
try {
WebElement elementName=driver.findElement(By.id(“Individual_Salutation”));
break;
} catch(StaleElementReferenceException e){
e.toString();
System.out.println(“Stale element error, trying :: ” + e.getMessage());
}
Upvotes: 0
Reputation: 443
Just works fine, you may want to add an implicit wait of say 3-5 secs since the application is quite slow.
Alternately, at the element .//*[@id='side-menu']/li[2]/a/span
you may also do a
driver.navigate().to("http://t1hub.govreports.com.au/App/#/Clients/MyClients");
I believe the place where you are getting the problem is ABN
. However it works good if appropriate wait is induced.
Upvotes: 1