1

I have a driver same code as below. And now I want to open a new tab and navigate my new tab to another url. But I get one Error

System.InvalidOperationException: missing field handle at line 3 column 1

DesiredCapabilities Capabilities = new DesiredCapabilities();
Capabilities.SetCapability(CapabilityType.BrowserName, "firefox");

string GridURL = "http://localhost:4545/wd/hub";
Driver = new RemoteWebDriver(new Uri(GridURL), Capabilities);
Driver.Navigate().GoToUrl("http://URL1.com");
IJavaScriptExecutor js = (IJavaScriptExecutor)Driver;
js.ExecuteScript("window.open('URL2.com', '_blank');");

// this line have error 
//System.InvalidOperationException: missing field `handle` at line 3 column 1
Driver.SwitchTo().Window(Driver.WindowHandles.First());
AsmusB
  • 5
  • 1

1 Answers1

1
  1. Make sure to use WebDriverWait class in order to ensure that second tab is open and ready and it's handle has been added to WindowHandles. See How to use Selenium to test web applications using AJAX technology article for more information on the concept.
  2. I don't see First() method in the ReadOnlyCollection so try accessing the handle in array-like manner

Example code:

WebDriverWait wait = new WebDriverWait(Driver, TimeSpan.FromSeconds(10));
wait.Until(d => d.WindowHandles.Count == 2);
Driver.SwitchTo().Window(Driver.WindowHandles[1]);
Console.WriteLine(Driver.Title);
Dmitri T
  • 159,985
  • 5
  • 83
  • 133
  • [First()](https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.first?view=netframework-4.8) is from Linq. And if the problem was that there was only one item in `WindowHandles` it would have thrown `index out of bounds exception`. – Guy Jul 23 '19 at 12:25
  • it is does not worked – user11824390 Jul 24 '19 at 13:10