1

I am using Selenium Webdriver with Java, and I need help handling sub child windows.

I have to handle four child windows.

Main window- Child window- Grand child - (to reach 5 child windows)

I am using code as follows, but facing problem on Grand child were controls are not at all working at any code change instances.

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Test {
public static void main(String[] args) throws InterruptedException {
 //System.setProperty("webdriver.ie.driver","E:\\Library\\IEDriverServer.exe");
 WebDriver driver = new FirefoxDriver();
 driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
 driver.get("https://70.91.43.177/rcsnewjs");
 driver.get("javascript:document.getElementById('overridelink').click();");
 driver.manage().window().maximize();
 driver.findElement(By.id("txtUserId")).clear();
 driver.findElement(By.id("txtUserId")).sendKeys("rcsadmin");
 driver.findElement(By.id("txtPassword")).sendKeys("delasoft");
 driver.findElement(By.id("btnLogin")).click();
 driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
 driver.findElement(By.id("__tab_TabContainer1_TabPanel3")).click();
 driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
 String MainWindow = driver.getWindowHandle();
 String Childwindow = MainWindow;
 String ChildWindow1 = Childwindow;
 String ChildWindow2 = ChildWindow1;
 try
 {
 driver.findElement(By.id("TabContainer1_TabPanel3_dmwmOutdoorAdvertising_btnAddPermits")).click();
 }
 catch(Exception e)
 {
 }
 for (String Handle : driver.getWindowHandles())
 {
 if (!Childwindow.equals(Handle))
 {
 Childwindow = Handle;
 driver.switchTo().window(Childwindow);
 Thread.sleep(5000);
 }
 }
 try
 {
 driver.findElement(By.id("btnSelectOwner")).click();
 }
 catch(Exception e)
 {
 }
 for (String Handle1 : driver.getWindowHandles())
 {
 if (!ChildWindow1.equals(Handle1))
 {
 ChildWindow1 = Handle1;
 driver.switchTo().window(ChildWindow1);
 Thread.sleep(5000);
 }
 }
 **Code is not reaching here. Which means controls are not working on the above opened window .**
 driver.findElement(By.xpath("html/body/form/div[3]/table/tbody/tr[1]/td/div/table/tbody/tr[4]/td/div/div[2]/div/table/tbody/tr[11]/td[5]/a")).click();
 Thread.sleep(2000);
 driver.switchTo().window(Childwindow);
 Thread.sleep(4000);
}
}

Thanks in advance.

asked Feb 9, 2015 at 6:28

4 Answers 4

2

To switch between windows there are multiple ways to do it. This is what we are using:

public void switchToNewWindow(int windowNumber) {
 Set < String > s = driver.getWindowHandles(); 
 Iterator < String > ite = s.iterator();
 int i = 1;
 while (ite.hasNext() && i < 10) {
 String popupHandle = ite.next().toString();
 driver.switchTo().window(popupHandle);
 System.out.println("Window title is : "+driver.getTitle());
 if (i == windowCount) break;
 i++;
 }
}

When you call this method just pass the number of windows you want to switch. Say for 2nd window: switchToNewWindow(2);

For 4th window: switchToNewWindow(4);

As getting window handle name is quite complex and it doesn't work some time because of spaces and special characters in window titles.

Working as engineers in automated testing services we have to cope up with dynamic contents on the web pages. Above method will work on all applications because we just need to pass the window number(occurrence number of window).

Hope this will help you.

answered May 6, 2016 at 7:33
2

You may have to switch to childWindow to do the operation on the same.

driver.switchTo().window(Childwindow);

you will have to use driver switch to.

You can switch between windows as below:

//Store the current window handle
String winHandleBefore = driver.getWindowHandle();
//Perform the click operation that opens new window
//Switch to new window opened
for(String winHandle : driver.getWindowHandles()){
 driver.switchTo().window(winHandle);
}
// Perform the actions on new window
//Close the new window if that window no more required
driver.close();
//Switch back to original browser (first window)
driver.switchTo().window(winHandleBefore);
//continue with original browser (first window)

For more details visit this site.

Bharat Mane
6,78512 gold badges42 silver badges69 bronze badges
answered Feb 10, 2015 at 6:21
5
  • Welcome to SQA stack exchange, Mayur. It's considered bad manners here to simply point to an answer you've made on another forum. Could you please edit your answer to hold all the information needed with the link as a reference? Commented Feb 10, 2015 at 12:00
  • Hi Paulk. Thank you for your pointer. I have edited my answer Commented Feb 10, 2015 at 12:16
  • I Want to perform actions on Second Level child window Commented Feb 10, 2015 at 13:38
  • Try this if below link works for you. mayurshah.in/115/selenium-webdriver-switch-window-using-title Commented Nov 17, 2016 at 9:55
  • Here is the fresh link: softwaretestingboard.com/qna/115/… Commented Nov 14, 2017 at 7:02
0

To switch to the second window....

int count =0;
String win = driver.getWindowHandle();
set<String> allwindows = driver.getWindowHandles();
for(String eachwindow : allwindows)
{
driver.switchTo().windoe(eachwindow);
count++;
if(count==2)
{
break;//control will be switched to second window
System.Out.Println(eachwindow);//will return the reference of second window
}
}

Above code switches to the second window when the count value is reached to 2 and returns the reference of second window.

IAmMilinPatel
7,7667 gold badges44 silver badges68 bronze badges
answered Nov 14, 2017 at 7:51
0

You can use the following code:

By Switching to the window we can achieve it:

  1. One way By Keep Switching by using if else nested condition

  2. Get all the Windows Id and as given below and Apply the condition to reach to your particular window

Below is sample example on Facebook...you can try..

public class Test5 {
 public static void main(String[] args) throws InterruptedException {
 System.setProperty("webdriver.chrome.driver", "/Users/pg/Downloads/chromedriver.exe");
 ChromeDriver driver=new ChromeDriver();
 driver.get("https://www.facebook.com/");
 driver.findElement(By.id("terms-link")).click();
 String mainWindow=driver.getWindowHandle();
 Set<String> allWindow=driver.getWindowHandles();
 Iterator<String> it=allWindow.iterator();
 while(it.hasNext())
 {
 String child=it.next();
 if(!mainWindow.equals(child))
 {
 driver.switchTo().window(child);
 driver.findElement(By.xpath("//*[@class='_42ft _4jy0 signup_btn _4jy4 _4jy2 selected _51sy']")).click();
 driver.close();
 driver.switchTo().window(mainWindow);
 driver.findElement(By.id("u_0_l")).sendKeys("Pankaj");
 }
 }
 }
}
answered Aug 7, 2019 at 10:34
0

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.