简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM