Search code examples
c#seleniumselenium-webdriverautomation

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


  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.

Solution

  • 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]);