Search code examples
c#selenium-webdrivervb.net-2010

How can I bring Chrome browser to focus when running a Selenium test using ChromeDriver in VB.NET and C#?


When executing a Selenium test with ChromeDriver, I would like to bring the test browser into focus to see the actions taking place. The way I am able to do that now is by manually clicking the instance of Chrome -- otherwise, the tests do run, I just don't see them taking place. I'd love for the focus part to happen automatically.

I have tried this with no luck:

  String currentWindow = driver.getWindowHandle();
  driver.switchTo().window(currentWindow);

Solution

  • I found the solution: In C# and VB.NET :

    driver.SwitchTo().Window(driver.WindowHandles(0))
    

    NOTE: The number 0 is the last opened tab. So if you have only one tab opens in browser, this code works fine. But if you have more than one tabs open, you should set this number as which tab you want to come up. For example if you want to open the tab which opened before last tab, you should set it to 1.