DWQA Ask QuestionCategory: QuestionsAssignmen-Level_Low: Multiple Window handling
admin Staff asked 3 years ago

Navigate: http://the-internet.herokuapp.com/windows

Part 1: Click on link.  Switch to new Window. Fetch text and print. Then close the other window.

Part 2: Change the attribute using java script executor and after that click on the link, so that page open in the same window.

4 Answers
Rahul Ugale Staff answered 3 years ago
public static void main(String[] args)
{
System.setProperty("webdriver.chrome.driver","C:\\Selenium\\chromedriver\\chromedriver_win32\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("http://the-internet.herokuapp.com/windows");

driver.findElement(By.xpath("//*[@id=\"content\"]/div/a")).click();

ArrayList<String> tabs = new ArrayList<String> (driver.getWindowHandles());
driver.switchTo().window(tabs.get(1));
WebElement str = driver.findElement(By.xpath("//h3"));
System.out.println(str.getText());
driver.close();
}
Pallavi Gadale Staff answered 3 years ago

Multiple Window Handling
public class Handle_multiple_windows {

public static void main(String[] args) throws Exception
{
WebDriver driver=new FirefoxDriver();
driver.get(” http://the-internet.herokuapp.com/windows&#8221;);
driver.manage().window().maximize();
//open new tab
WebElement handleWindow=driver.findElement(By.partialLinkText(“Click Here”));
handleWindow.click();
//handle window
ArrayList<String> tabs = new ArrayList<String>(driver.getWindowHandles());
System.out.println(tabs.size());
//switch to second tab
driver.switchTo().window(tabs.get(1));
//click on new tab
WebElement new_window = driver.findElement(By.xpath(“//div[h3=’New Window’]”));
new_window.click();

Thread.sleep(3000);
//closing only new tab
driver.close();
//return to old window
driver.switchTo().window(tabs.get(0));

}

Rahul Staff answered 3 years ago
Start your code here
public class WindowHandle {
WebDriver driver;
@Test
public void f() {
System.getProperty("webdriver.chrome.driver","D:\\chromedriver.exe");
driver =new ChromeDriver();
driver.manage().deleteAllCookies();
driver.manage().window().maximize();
driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

driver.get(" http://the-internet.herokuapp.com/windows");

String mainwindow=driver.getWindowHandle();

//a[@href='/windows/new']
driver.findElement(By.xpath("//a[@href='/windows/new']")).click();
Set<String>set=driver.getWindowHandles();
for(String childwindow:set)
{
if(!mainwindow.equals(childwindow))
{
driver.switchTo().window(childwindow);
WebElement ele=driver.findElement(By.tagName("h3"));
System.out.println(ele.getText());
driver.close();
}
}
driver.switchTo().window(mainwindow);
}
}

Suraj Gaikwad Staff answered 3 years ago

Multiple Window Handling code 

driver.get("http://the-internet.herokuapp.com/windows");

WebElement click_here_link = driver.findElement(By.xpath("//a[text()='Click Here']"));
click_here_link.click();

ArrayList<String> tabs = new ArrayList<String>(driver.getWindowHandles());
driver.switchTo().window(tabs.get(1));

WebElement text = driver.findElement(By.xpath("//h3[text()='New Window']"));
System.out.println(text.getText());

driver.close();