This is a continuation of the previous question that I asked here.
Earlier, I ran into the problem of switching windows in Internet Explorer 10 using Selenium 2.37.0. My C # program will go to the page, click the button that opens the link, and try to go to a new window to perform additional tasks. My program did not work, and I decided that this was due to the processing of Selenium windows. After opening a new window, the number of window descriptors ( driver.WindowHandles.Count) increases from 1 to 2, as expected, but after a while the number of window descriptors drops to 1.
I have created a minimal sample web page that reproduces this problem. Just save the following code in a file with a name test.aspxon your desktop:
<html xmlns="http://www.w3.org/1999/xhtml" >
<body>
<input id="btn" type="button" value="Link" lang="javascript" onclick="window.open('test.aspx')">
</body>
</html>
Here is my C # code (a console project in Visual Studio 2010 called TestWindowSwitching) that opens this page, presses a button and prints some output:
using System;
using System.Threading;
using System.Diagnostics;
using OpenQA.Selenium;
using OpenQA.Selenium.IE;
namespace TestWindowSwitching
{
class Program
{
static void Main()
{
IWebDriver driver = new InternetExplorerDriver();
driver.Navigate().GoToUrl("C:\\Users\\yourNameHere\\Desktop\\test.aspx");
try { driver.SwitchTo().DefaultContent(); }
catch { Thread.Sleep(10); }
Console.WriteLine("Initial number of window handles: " + driver.WindowHandles.Count);
Stopwatch sw = new Stopwatch();
try { driver.FindElement(By.Id("btn")).Click(); }
catch { Thread.Sleep(10); }
while (driver.WindowHandles.Count != 2)
{
}
sw.Start();
while (driver.WindowHandles.Count == 2)
{
}
Console.WriteLine("Number of window handles has dropped to " + driver.WindowHandles.Count
+ " in " + sw.ElapsedMilliseconds + " ms");
sw.Stop();
Console.Write("Press Enter to close...");
Console.ReadLine();
driver.Quit();
}
}
}
Here is an example output for this code:
Starting window descriptors: 1
The number of window handles dropped to 1 in 59 ms
Press Enter to close ...
I previously created a workaround for this, so for me this is no longer a problem. I just get the Javascript command (in this case, window.open('test.aspx')) and use it directly to go to the page without opening a new window.
, , , , , ( 1, 2), , Selenium, . , - , .