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.
4 Answers 4
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.
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.
-
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?Kate Paulk– Kate Paulk2015年02月10日 12:00:33 +00:00Commented Feb 10, 2015 at 12:00
-
Hi Paulk. Thank you for your pointer. I have edited my answerMayur Shah– Mayur Shah2015年02月10日 12:16:42 +00:00Commented Feb 10, 2015 at 12:16
-
I Want to perform actions on Second Level child windowSateesh Sasapu– Sateesh Sasapu2015年02月10日 13:38:27 +00:00Commented Feb 10, 2015 at 13:38
-
Try this if below link works for you. mayurshah.in/115/selenium-webdriver-switch-window-using-titleMayur Shah– Mayur Shah2016年11月17日 09:55:55 +00:00Commented Nov 17, 2016 at 9:55
-
Here is the fresh link: softwaretestingboard.com/qna/115/…Mayur Shah– Mayur Shah2017年11月14日 07:02:40 +00:00Commented Nov 14, 2017 at 7:02
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.
You can use the following code:
By Switching to the window we can achieve it:
One way By Keep Switching by using if else nested condition
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");
}
}
}
}
Explore related questions
See similar questions with these tags.