java - Switch from child to parent window in Selenium Webdriver -
i'm creating test scripts in selenium websriver using eclipse, , have nail snag in scenario whereby have parent window, click on link on window , kid window opens. want close kid window , carry out functions 1 time again on parent window.
my code below:
public static void daysinstockselectcontract(internetexplorerdriver driver) { driver.findelement(by.xpath(".//*[@id='page-content']/table/tbody/tr[1]/td[1]/a")).click(); for(string winhandle : driver.getwindowhandles()) { driver.switchto().window(winhandle); driver.close(); } }
when run above code, kid window remains open whilst parent window closes, opposite effect of wanted. error follows shown in console:
"exception in thread "main" org.openqa.selenium.remote.sessionnotfoundexception: session 1ff55fb6-71c9-4466-9993-32d7d9cae760 not exist"
i'm using ie webdriver selenium scripts.
update - 17/11/14
subh, here code used link kindly send on unfortunately doesn't appear work.
public static void daysinstockselectcontract(internetexplorerdriver driver) { //get parent handle before clicking on link string winhandlebefore = driver.getwindowhandle(); driver.findelement(by.xpath(".//*[@id='page-content']/table/tbody/tr[1]/td[1]/a")).click(); // set contain kid window now. switch kid window , close it. for(string winhandle : driver.getwindowhandles()) { driver.switchto().window(winhandle); } driver.close(); driver.switchto().window(winhandlebefore); }
probably, switching kid window didn't happen properly.
hence, 'driver.close()' closing parent window, instead of kid window. also, since parent window has been closed, implies session has been lost gives error 'sessionnotfoundexception'.
this link help out in properly switching between windows
on note, advice. rather passing "driver" parameter, why don't create static variable. accessible methods within class , it's subclasses too, , don't have bother passing each time method.. :)
below code you've requested in comment (unrelated question above)
public class testing_anew { public static webdriver driver; public static void main(string args[]) throws interruptedexception{ driver = new firefoxdriver(); driver.manage().window().maximize(); driver.manage().timeouts().implicitlywait(30, timeunit.seconds); } public static void testmethod(){ driver.findelement(by.xpath("//some xpath")).click(); }
updated code 19/11/14
public static void daysinstockselectcontract(internetexplorerdriver driver) { //get parent handle before clicking on link string winhandlebefore = driver.getwindowhandle(); system.out.println("current handle is: "+winhandlebefore); driver.findelement(by.xpath(".//*[@id='page-content']/table/tbody/tr[1]/td[1]/a")).click(); // iterating through set of window handles till kid window's handle, infact // not equal current window handle, found for(string winhandle : driver.getwindowhandles()) { if(!winhandle.equals(winhandlebefore)){ system.out.println("child handle is: "+ winhandle); //sleeping 4 seconds detecting kid window try{ thread.sleep(4000); }catch(interruptedexception e){ system.out.println("caught exception related sleep:"+e.getmessage()); } driver.switchto().window(winhandle); break; } } system.out.println("after switching, handle is: "+driver.getwindowhandle()); driver.close(); //sleeping 4 seconds detecting parent window try{ thread.sleep(4000); }catch(interruptedexception e){ system.out.println("caught exception related sleep:"+e.getmessage()); } driver.switchto().window(winhandlebefore); // switching parent window system.out.println("now current handle is: "+driver.getwindowhandle()); //now perform user action here respect parent window assert window has switched }
java eclipse selenium selenium-webdriver
No comments:
Post a Comment