webdriver.switchTo().window() doesnt work here

104 views
Skip to first unread message

Old Keksi

unread,
Mar 23, 2022, 9:46:32 AM3/23/22
to Selenium Users
Hello all

I'm running some experiments with Selenium 4 using Java on a Mac. I found out that the switchTo().window() statement doesn't work as described. Here's the code:

import org.openqa.selenium.chrome.ChromeDriver;
import java.util.Collection;

public class SwitchDoesntWork
{
    public static void main(String... args)
    {
        System.setProperty("webdriver.chrome.driver", "./lib/chromedriver 99");
        ChromeDriver webDriver = new ChromeDriver();

        try
        {
            webDriver
                .navigate()
                .to("file:///(...)/src/test/resources/switchDoesntWorkMain.html");

            webDriver
                .navigate()
                .to("file://(...)/src/test/resources/switchDoesntWorkSub1.html");
            //            webDriver.findElement(By.id("link1")).click();    // This doesn't work either

            String mainWindowHandle = webDriver.getWindowHandle();
            Collection<String> allWindowHandles = webDriver.getWindowHandles();
            System.out.println("Main Handle: " + mainWindowHandle);
            System.out.println("All Handles: " + allWindowHandles);         //  Contains the main handle only
            System.out.println("All size:    " + allWindowHandles.size());  //  This returns 1 - there is no 2nd handle

            for (String handle: allWindowHandles)
            {
                if (!handle.equals(mainWindowHandle))
                {
                    System.out.println("Switching to handle: " + handle);
                    webDriver.switchTo().window(handle);        //  He never gets here
                }
            }
        }
        finally
        {
            webDriver.quit();
        }
    }
}

The two html pages referenced are very simple:

switchDoesntWorkMain.html:
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/html">
<head>
    <meta charset="UTF-8">
    <link rel="stylesheet" href="style.css"/>
    <title>Switch Doesn't work - MAIN</title>
</head>
<body>
<h1>Main Page</h1>
<p>
<div>
    <a id="link1" href="switchDoesntWorkSub1.html">Subpage</a>
</div>
</body>
</html>

switchDoesntWorkSub1.html:
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8"/>
    <link rel="stylesheet" href="style.css"/>
    <title>Switch Doesn't work - SUB1</title>
</head>
<body>
<h1>Subpage</h1>
<p>
<div>
    <a id="linkBack" href="switchDoesntWorkMain.html">Back</a>
</div>
</body>
</html>

In ./lib/ is the current chromedriver as downloaded from <chromedriver.storage.googleapis.com/index.html?path=99.0.4844.51/>

What's wrong with my code?

Old Keksi

unread,
Mar 23, 2022, 10:50:01 AM3/23/22
to Selenium Users
Actually the subject is wrong. The webdriver.switchTo().window() statement is not the problem. The problem is that the webDriver.getWindowHandles() statement doesn't return the handle of the newly opened window so that I can't switch to it.

Veena

unread,
Mar 28, 2022, 12:59:09 AM3/28/22
to Selenium Users
both webdriver.navigate opens a given html file in the same window, So getWindowHanles returns a single window.You must open a new window or tab before launch new URL 

use webdriver.switchTo().newWindow(WindowType.TAB);  between your two webdriver.navigate, so the second URL gets open in a new tab, and if you want to access your first webpage then you have to use
driver.switchTo().defaultContent();

Cheers
Veena

Old Keksi

unread,
Mar 28, 2022, 10:52:48 AM3/28/22
to Selenium Users
That's it: a new window tab.

many great thanx to you, Veena! 

Reply all
Reply to author
Forward
0 new messages