How to switch between two windows in browser using Selenium java

0
0

I’m working with Selenium Automation. In this, When i click a link in a current window, a new window opens. I just want to switch the control to the new window. But i can’t do this.Actually the new window is an auto-generated one. That is, link will be generated dynamically. Help me friends…

  • You must to post comments
0
0

I finally found the answer,
I used the below method to switch to the new window,

public String switchwindow(String object, String data){
try {
String winHandleBefore = driver.getWindowHandle();
for(String winHandle : driver.getWindowHandles()){
driver
.switchTo().window(winHandle);
}
}catch(Exception e){
return Constants.KEYWORD_FAIL+ "Unable to Switch Window" + e.getMessage();
}
return Constants.KEYWORD_PASS;
}

To move to parent window, i used the following code,

 public String switchwindowback(String object, String data){
try {
String winHandleBefore = driver.getWindowHandle();
driver
.close();
//Switch back to original browser (first window)
driver
.switchTo().window(winHandleBefore);
//continue with original browser (first window)
}catch(Exception e){
return Constants.KEYWORD_FAIL+ "Unable to Switch to main window" + e.getMessage();
}
return Constants.KEYWORD_PASS;
}
  • You must to post comments
Showing 1 result
Your Answer
Post as a guest by filling out the fields below or if you already have an account.
Name*
E-mail*
Website