selenium - How to handle the pop window to access the pop elements -
i'm having testcase handle popup control not going popup window. displaying gettitle of main window instead of popup window. can go through below code.
@test public void testtext1() throws exception { driver.get("http://www.hdfcbank.com"); thread.sleep(8000); driver.findelement(by.xpath(".//*[@id='loginsubmit']")).click(); string popuphandle = driver.getwindowhandle(); webdriver popup; popup = driver.switchto().window(popuphandle); system.out.println(popup.gettitle()); if (popup.gettitle().equals("netbanking")) { system.out.println("i going access elements of popup"); driver.findelement(by.xpath(".//*[@id='wrapper']/div[6]/a/img")).click(); } else { system.out.println("worth trying try harder success"); // } }
output: {d0f39d30-49e7-4203-b9ef-10380fbfcb5e} hdfc bank: personal banking services going access elements of popup exception in thread "main" org.openqa.selenium.nosuchelementexception: unable locate element: {"method":"xpath","selector":".//*[@id='wrapper']/div[6]/a/img"} command duration or timeout: 30.15 seconds
you're on right track. driver.getwindowhandle()
method returns handle current window, main window. if pop-up window opens, need call set<string> handles = driver.getwindowhandles()
return list of available handles. can call driver.switchto().window(handles.get(handles.size() - 1))
. switch last listed (newest) window handle.
you don't need declare new webdriver object. calling switchto()
, transferring driver's focus new window. make sure save reference main window's handle before making switch able switch main window afterwards.
string mainhandle = driver.getwindowhandle(); string[] handles = driver.getwindowhandles().toarray(new string[0]); driver.switchto().window(handles[handles.length - 1]); ... driver.close(); //close popup window driver.switchto().window(mainhandle);
Comments
Post a Comment