Sunday, January 26, 2020

Switch to new open window in Selenium Java

0 comments

How to get switch between Parent and Child Window.
Parent=The Current Window.
Child=That window which was opened after clicking on Link.


********************CODE*******************

package aveditor;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;



public class SwitchToChild {
public static void main(String []args)
{
WebDriver driver;
System.setProperty("webdriver.chrome.driver", ".\\ChromeDriverPath\\chromedriver.exe");
driver=new ChromeDriver();
driver.get("https://automaticselenium.blogspot.com/p/example-switch.html");
driver.manage().window().maximize(); //Maximize the Window after Opening the Links
// Getting the Parent Window 
String ParentWindow=driver.getWindowHandle();
driver.findElement(By.linkText("Click here for New Window")).click();
// Switching that window which is opened
for(String window:driver.getWindowHandles())
{
driver.switchTo().window(window);
}
}
}

------------------------------------------------------------------------------------------------
Switching first to the Child Window then again Back switch to the Parent Window


********************CODE*******************
package aveditor;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;



public class NewTest {
public static void main(String []args) throws Exception
{
WebDriver driver;
System.setProperty("webdriver.chrome.driver", "C:\\Users\\oracle\\Music\\ChromeDriverPath\\chromedriver.exe");
driver=new ChromeDriver();
driver.get("https://automaticselenium.blogspot.com/p/example-switch.html");
driver.manage().window().maximize(); //Maximize the Window after Opening the Links
// Getting the Parent Window 
String ParentWindow=driver.getWindowHandle();
driver.findElement(By.linkText("Click here for New Window")).click();
// Switching that window which is opened
for(String window:driver.getWindowHandles())
{
driver.switchTo().window(window);
}
// Putting Thread to see the switching between parent and child window
Thread.sleep(10000);
// Switching back to the Parent Window
driver.switchTo().window(ParentWindow);
}
}















No comments:

Post a Comment