java - How to handle "StaleElementReferenceException" - Chrome browser -
i testing newly built framework. encountering org.openqa.selenium.staleelementreferenceexception while working in chrome browser. there issue framework design. in case there no issues if run tests in other browsers. tried many types of custom waits catches staleelementreferenceexception , loops finding element, no luck. have faced similar issue , have solution this?
fyi.... chrome version: 38.0.2125.111 selenium version: 2.43.1
public webelement waittill(by by){ webelement ele = null; for(int i=0; i<15; i++){ seek { ele = driver.findelement(by); if(ele==null) thread.sleep(2000); //in lastly effort used thread...we wont utilize in actual practice else break; } grab (nosuchelementexception | interruptedexception e) { system.out.println(e.getmessage()); } } homecoming ele; } public webelement getelement(string loc) { string locator = initutils.orprop.getproperty(loc); webelement element = null; try{ by = getby(locator); element = waittill(by); }catch(nosuchelementexception e){ system.out.println(e.getmessage()); }catch(staleelementreferenceexception e){ by = getby(locator); element = waittill(by); } homecoming element; }
this sort of error caused webpage changing in between times element checked.
this exception caused bad tests afraid say. few things check are:
make sure giving page time load when go before start interacting it, if think has finished loading may still waiting in background , when arrives page changes.
make sure if interact element changes page create sure 1 time again wait page alter , html requests process.
these 2 things may cause of of problems, recommend using ajax uses jquery ajax start , stop in order create sure page loaded before modifying it. need remember selenium much faster user perchance interact page , need handle increasing checks.
i recommend checking whether element on page , visible before trying interact it.
in worse case senario utilize seek , grab block check element if create sure page not changing shouldnt exception. differ between browsers due browser speed , webdriver speed.
some of code utilize is:
var finished = false; function ready() { if (finished == true) { $( "#main" ).addclass("ready"); } } $( document ).ajaxstart(function() { $( "#main" ).removeclass("ready"); finished = false; }); $( document ).ajaxstop(function() { finished = true; window.settimeout(ready,500); });'
this checks page loaded , no requests pending, execute 1 time browser open, can check whether class nowadays , if ready go. phone call same check whenever page changes well.
java selenium selenium-webdriver
No comments:
Post a Comment