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?