java - How to avoid StaleElementReferenceException -
public long searchhotel(string hotelname,string provider,string cookiename) throws interruptedexception, parseexception{ long day = 0; webdriver driver =new firefoxdriver(); driver.manage().timeouts().implicitlywait(5000, timeunit.milliseconds); driver.get("url"); driver.manage().window().maximize(); driver.findelement(by.id("hc_f_id_where_1")).sendkeys(hotelname); list<webelement> hl = driver.findelements(by.id("ui-id-1")); webdriverwait wait = new webdriverwait(driver,30); wait.until(expectedconditions.visibilityofallelementslocatedby(by.id("ui-id-1"))); string startdate = null; string enddate = null; date d1,d2; calendar firstdate =null; calendar seconddate =null; org.joda.time.datetime dt1; org.joda.time.datetime dt2; long diffinmillisec=0; long diffindays=0; //datetime dt2; simpledateformat dateformat = null; (webelement e : hl) { if(e.gettext().contains(hotelname)) { system.out.println("in if"); driver.findelement(by.partiallinktext(hotelname)).click(); } } driver.findelement(by.id("hc_dateselection_checkin_1")).click(); driver.findelement(by.cssselector("img.ui-datepicker-trigger")).click(); webelement datewidget = driver.findelement(by.cssselector("div.ui-datepicker-group.ui-datepicker-group-last table.ui-datepicker-calendar")); list<webelement> columns=datewidget.findelements(by.tagname("td")); (webelement cell : columns) { //system.out.println(cell.gettext()); if(cell.gettext().equals("7")) { cell.findelement(by.linktext("7")).click(); break; } } driver.findelement(by.id("hc_dateselection_checkout_1")).click(); driver.findelement(by.cssselector("div#hc_dateselection_checkout_1 img.ui-datepicker-trigger")).click(); webelement datewidget1 = driver.findelement(by.cssselector("div.ui-datepicker-group.ui-datepicker-group-first table.ui-datepicker-calendar")); list<webelement> columns1 = datewidget1.findelements(by.tagname("td")); (webelement cell1 : columns1) { //system.out.println(cell1.gettext()); if(cell1.gettext().equals("11")) { cell1.findelement(by.linktext("11")).click(); break; } } driver.findelement(by.classname("hc_f_btn_v15")).click(); driver.findelement(by.classname("hc_usi")).click(); system.out.println("waiting"); webelement hotellist=driver.findelement(by.cssselector("table#hc_htl_pm_rates_content")); list<webelement> hl=hotellist.findelements(by.tagname("tr")); (webelement h : hl) { if(provider.equals(h.getattribute("data-providername"))){ system.out.println("provider name : "+h.getattribute("data-providername").tostring()); wait<webdriver> fwait = new fluentwait<webdriver>(driver).withtimeout(30,timeunit.seconds).pollingevery(5,timeunit.seconds).ignoring(staleelementreferenceexception.class); fwait.until(expectedconditions.elementtobeclickable(h.findelement(by.tagname("a")))).click(); h.findelement(by.tagname("a")).click(); break; } } set<string> winhandles=driver.getwindowhandles(); (string window : winhandles) { driver.switchto().window(window); } wait.until(expectedconditions.visibilityofelementlocated(by.tagname("h1"))); system.out.println("page title : "+driver.gettitle()); string cururl=driver.getcurrenturl(); system.out.println("current url : "+cururl); dateformat = new simpledateformat("mm/dd/yyyy"); set<cookie> cookies = driver.manage().getcookies(); iterator<cookie> itr = cookies.iterator(); while (itr.hasnext()) { cookie c = itr.next(); system.out.println("-----cookies detail-----"); system.out.println("cookie name: " + c.getname() + "\n\tcookie domain: " + c.getdomain() + "\n\tcookie value: " + c.getvalue() + "\n\tpath: " + c.getpath()+ "\n\texpiry date: " + c.getexpiry()+ "\n\tsecure: " + c.issecure()); if(cookiename.equalsignorecase(c.getname())) { system.out.println("cookies found : " +cookiename); try { enddate = c.getexpiry().tostring(); simpledateformat format = new simpledateformat("e mmm dd hh:mm:ss z yyyy"); //simpledateformat format = new simpledateformat("mm/dd/yyyy"); d1=format.parse(enddate); system.out.println(d1); d2=new date(); startdate=d2.tostring(); d2=format.parse(startdate); system.out.println(d2); dt1 = new org.joda.time.datetime(d1); dt2 = new org.joda.time.datetime(d2); day=days.daysbetween(dt2,dt1).getdays(); firstdate = calendar.getinstance(); seconddate = calendar.getinstance(); firstdate.settime(d1); seconddate.settime(d2); int month = firstdate.get(calendar.month)+1; int dt = firstdate.get(calendar.date); int year = firstdate.get(calendar.year); firstdate.set(year, month, dt); system.out.println("first dare "+dt+"/"+month+"/"+year); int month2=seconddate.get(calendar.month)+1; int dt22 = seconddate.get(calendar.date); int year2= seconddate.get(calendar.year); seconddate.set(year2, month2, dt22); system.out.println("second date "+dt22+"/"+month2+"/"+year2); diffinmillisec = firstdate.gettimeinmillis() - seconddate.gettimeinmillis(); diffindays = diffinmillisec / (24 * 60 * 60 * 1000); system.out.println("days : "+diffindays); } catch(exception e) { e.printstacktrace(); } } } driver.quit(); return diffindays;
}
getting exception in thread "main" org.openqa.selenium.staleelementreferenceexception: element not found in cache - perhaps page has changed since looked command duration or timeout: 5.50 seconds
Comments
Post a Comment