Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to switch between two windows in browser using Selenium java

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...

like image 878
Prasanna Avatar asked Oct 01 '13 13:10

Prasanna


People also ask

How do I switch windows in Java?

First, you need to save the reference to the current window. String parentWindow= driver. getWindowHandle(); The after having clicked the link, you need to switch to the other window.

How do I open multiple windows in Selenium?

Open Multiple Tabs Use One WebDriver Object. Send "Ctrl+t" command to body element to open a new browser tab. Send "Ctrl+2" command to navigate to the second browser tab. Change the URL to google.com for the second browser tab. Get the body web element in the second browser tab.


2 Answers

To switch between windows we have method.

driver.switchTo().window("window name") 

To get the different windows handle, we have method.

driver.getWindowHandles()

Example:

    File file = new File("G:\\Selenium\\All_Jars\\chromedriver.exe");
    System.setProperty("webdriver.chrome.driver",file.getAbsolutePath() );
    driver = new ChromeDriver();

    //Maximize the window
    driver.manage().window().maximize();

    driver.get("http://www.rediff.com/");

    //Get all window handles
    Set<String> allHandles = driver.getWindowHandles();

    //count the handles Here count is=2
    System.out.println("Count of windows:"+allHandles.size());      

    //Get current handle or default handle
    String currentWindowHandle = allHandles.iterator().next();
    System.out.println("currentWindow Handle"+currentWindowHandle);

    //Remove first/default Handle
    allHandles.remove(allHandles.iterator().next());

    //get the last Window Handle
    String lastHandle = allHandles.iterator().next();
    System.out.println("last window handle"+lastHandle);

    //switch to second/last window, because we know there are only two windows 1-parent window 2-other window(ad window)
driver.switchTo().window(lastHandle);
    System.out.println(driver.getTitle());
    driver.findElement(By.tagName("body")).click();
like image 109
Avinash Pande Avatar answered Sep 21 '22 02:09

Avinash Pande


Yes this is possible. First you need to save the refrence to current window.

String parentWindow= driver.getWindowHandle();

The after having clicked the link, you need to switch to the other window.

List<String> allWindows = driver.getWindowHandles();
for(String curWindow : allWindows){
    driver.switchTo().window(curWindow);
}

This is where you perform operations on new window, finally closing it with

driver.close();

and switch back to parent window

driver.switchTo().window(parentWindow);
like image 45
SpartanElite Avatar answered Sep 19 '22 02:09

SpartanElite