Bacancy Technology
Bacancy Technology represents the connected world, offering innovative and customer-centric information technology experiences, enabling Enterprises, Associates and the Society to Rise™.
12+
Countries where we have happy customers
1050+
Agile enabled employees
06
World wide offices
12+
Years of Experience
05
Agile Coaches
14
Certified Scrum Masters
1000+
Clients projects
1458
Happy customers
Artificial Intelligence
Machine Learning
Salesforce
Microsoft
SAP
October 9, 2023
Rather than storing references to elements, it is important to locate and interact with them dynamically to avoid the StaleElementReferenceException. To do this, one must use a combination of explicit waits and polling to locate elements. Then, the element must be interacted with immediately after locating it to avoid the exception. This can be done by using the appropriate methods available in the Selenium WebDriver API.
Code example one:
Try to refresh the page before send the keys.
driver.navigate().refresh(); driver.findElement(By.id("APjFqb")).sendKeys("1234567");
Code example two:
Use try catch and in catch block try to initiate a variable.
try { driver.get("https://www.google.com"); driver.findElement(By.id("APjFqb")).sendKeys("1234567"); System.out.println("method f id:"+Thread.currentThread().getId()+" n:"+n+" s:"+s); }catch(StaleElementReferenceException e) { driver.findElement(By.id("APjFqb")).sendKeys("1234567"); }