How to open a new tab in the existing Firefox browser using Selenium WebDriver (a.k.a. Selenium 2)?
Handling browser window using Selenium Webdriver:
String winHandleBefore = driver.getWindowHandle();
for(String winHandle : driver.getWindowHandles()) // Switch to new opened window
{
driver.switchTo().window(winHandle);
}
driver.switchTo().window(winHandleBefore); // move to previously opened window
The code below will open the link in new Tab.
String selectLinkOpeninNewTab = Keys.chord(Keys.CONTROL,Keys.RETURN);
driver.findElement(By.linkText("urlLink")).sendKeys(selectLinkOpeninNewTab);
The code below will open empty new Tab.
String selectLinkOpeninNewTab = Keys.chord(Keys.CONTROL,"t");
driver.findElement(By.linkText("urlLink")).sendKeys(selectLinkOpeninNewTab);
Why not do this
driver.ExecuteScript("window.open('your url','_blank');");
How to open a new tab using Selenium WebDriver with Java for chrome?
ChromeOptions options = new ChromeOptions();
options.addArguments("--disable-extensions");
driver = new ChromeDriver(options);
driver.manage().window().maximize();
driver.navigate().to("https://google.com");
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_T);
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.keyRelease(KeyEvent.VK_T);
Above code will disable first extensions and using robot class new tab will open.
Below code will open the link in new window
String selectAll = Keys.chord(Keys.SHIFT,Keys.RETURN);
driver.findElement(By.linkText("linkname")).sendKeys(selectAll);
How to open a new, but more importantly, how do you do stuff in that new tab? Webdriver doesn't add a new WindowHandle for each tab, and only has control of the first tab. So, after selecting a new tab (Control + Tab Number) set .DefaultContent() on the driver to define the visible tab as the one you're going to do work on.
Visual Basic
Dim driver = New WebDriver("Firefox", BaseUrl)
' Open new tab - send Control T
Dim body As IWebElement = driver.FindElement(By.TagName("body"))
body.SendKeys(Keys.Control + "t")
' Go to a URL in that tab
driver.GoToUrl("YourURL")
' Assuming you have m tabs open, go to tab n by sending Control + n
body.SendKeys(Keys.Control + n.ToString())
' Now set the visible tab as the drivers default content.
driver.SwitchTo().DefaultContent()