October 14, 2022

How to switch to next tab in browser programmatically without using tab name (Selenium + .Net)

  October 14, 2022

There will be several occasions while automating a web application where we need to move our control to the next tab.

This may occur if the link is opened in a new tab or if the pop-up is opened as a new tab etc.

But if we don't know the title of the new tab, we can't easily switch to the new tab and do WebDriver actions there.

Below is a simple code to switch tabs in the browser using selenium with only the index number of the tab.

Once we are switched to a new tab, we can get the tab title from there which can be used in further steps of the test.

/// <summary>
/// Method to change focus to first child window
/// </summary>
public void switchFocusToNextWindow()
{
	driver.SwitchTo().Window(driver.WindowHandles.ElementAt(1));
	Console.WriteLine("Focus changed to new window with name as  " + driver.Title);
}
logoblog

Thanks for reading How to switch to next tab in browser programmatically without using tab name (Selenium + .Net)

Previous
« Prev Post

No comments:

Post a Comment

Bookmark this website for more workarounds and issue fixes.

Verify Zip file contents without extracting using C# + Selenium

While doing automation testing, we may get a scenario where we need to download a zip file and to validate the contents inside it. One way t...