How to open a new tab using Selenium WebDriver in C#?
Asked Answered
D

1

5
  1. webElement.SendKeys(Keys.Control + "t"); This code is not working for me.
  2. String n = Keys.chord(Keys.CONTROL, Keys.ENTER); driver.findElement(By.id("open-tab")).sendKeys(n); In which key.chord is not working for selenium C#.
  3. driver.SwitchTo().Window(driver.WindowHandles[0]); this one is also not working with my code. Is any alternative way available for switching tab.
Dalton answered 19/3, 2022 at 12:0 Comment(0)
A
9

Selenium 4 solution:

driver.SwitchTo().NewWindow(WindowType.Tab);

Note that it will open a new tab in the same window and will switch also to the newly opened tab.

to open a new window, you should use:

driver.SwitchTo().NewWindow(WindowType.Window);

Selenium 3 solution:

((IJavaScriptExecutor)driver).ExecuteScript("window.open()");
List<string> tabs = new List<string> (driver.WindowHandles);
driver.SwitchTo().Window(tabs[1]);
Acth answered 19/3, 2022 at 14:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.