-1

How to change tab with selenium? I am performing my automatic tests but when I click on a button, it opens a new tab. What I need is that selenium change tab and continue with automation. I'm using

String selectLinkOpeninNewTab = Keys.chord(Keys.CONTROL,"2");

But it does not work and the code fails.

badjr
  • 2,166
  • 3
  • 20
  • 31
Steven
  • 1
  • 1

1 Answers1

1

C#

Use this code to open new tab & switch between tabs:

Use Waiter.

tabs index starts from 0 for 1st tab.

var body = Waiter.Until(ExpectedConditions.PresenceOfAllElementsLocatedBy(By.TagName("body"))).FirstOrDefault();
body.SendKeys(Keys.Control + 't');     //Opens new tab
var tabs = GlobalDriver.WindowHandles;
GlobalDriver.SwitchTo().Window(tabs[1]);
GlobalDriver.Navigate().GoToUrl("Url");

The code is in C#

It will be somehow similar in Java, just the syntax will be different.

Hope this helps you!

Mohsin Awan
  • 1,176
  • 2
  • 12
  • 29