Switching to Current Window in Selenium using C#

As we test different webpages in Automation sometimes we may encounter scenario where we may need to switch over to different frames on Windows to test ,click and verify the elements on those webpages or frames. So in this case we may have to make use of the Switch to Current Window functionality in C#

 

//This method makes Switch To Current Window makes much easier 
public void SwitchtoCurrentWindow()
{
       //SwitchTo is Seleniums InBuilt Code and Driver is WebDriver 
        Driver.SwitchTo().Window(Driver.WindowHandles.Last());
 }

The above code does these

  • You click on a link and the link opens in a new Window or tab
  • You may need to verify the elements on the link which has just opened in a new tab
  • So to switch to the new tab that has opened you can use the above code
  • The above code always switches to current active window the focus is in

 

You may also like...