How to open a new tab in the existing Firefox browser using Selenium WebDriver (a.k.a. Selenium 2)?
I am using Selenium 2.52.0 in Java and Firefox 44.0.2. Unfortunately none of above solutions worked for me. The problem is if I a call driver.getWindowHandles() I always get 1 single handle. Somehow this makes sense to me as Firefox is a single process and each tab is not a separate process. But maybe I am wrong. Anyhow I try to write my own solution:
// open a new tab
driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL + "t");
//url to open in a new tab
String urlToOpen = "https://url_to_open_in_a_new_tab";
Iterator<String> windowIterator = driver.getWindowHandles()
.iterator();
//I always get handlesSize == 1, regardless how many tabs I have
int handlesSize = driver.getWindowHandles().size();
//I had to grab the original handle
String originalHandle = driver.getWindowHandle();
driver.navigate().to(urlToOpen);
Actions action = new Actions(driver);
// close the newly opened tab
action.keyDown(Keys.CONTROL).sendKeys("w").perform();
// switch back to original
action.keyDown(Keys.CONTROL).sendKeys("1").perform();
//and switch back to the original handle. I am not sure why, but
//it just did not work without this, like it has lost the focus
driver.switchTo().window(originalHandle);
I used Ctrl+t combination to open a new tab, Ctrl+w to close it, and to switch back to original tab I used Ctrl+1 (the first tab). I am aware that mine solution is not perfect or even good and I would also like to switch with driver's switchTo call, but as I wrote it was not possible as I had only one handle. Maybe this will be helpful to someone with the same situation.
Due to bug in https://bugs.chromium.org/p/chromedriver/issues/detail?id=1465 even though webdriver.switchTo actually does switch tabs, the focus is left on the first tab. You can confirm this by doing a driver.get after the switchWindow and see that the second tab actually go to the new URL and not the original tab.
Work around for now is what @yardening2 suggested. Use js to open an alert and then use webdriver to accept it.
Selenium doesn't support opening new tabs, it only supports opening new windows. For all intents an purposes a new window is functionally equivalent to a new tab anyway.
There are various hacks to work around the issue but they are going to cause you other problems in the long run.
WebDriver now has support for opening tabs:
browser = Selenium::WebDriver.for :chrome
new_tab = browser.manage.new_window
Will open a new tab. Opening a window has actually become the non-standard case:
browser.manage.new_window(:window)
The tab or window will not automatically be focussed. To switch to it:
browser.switch_to.window new_tab
To open a new tab in the existing Firefox browser using Selenium WebDriver
FirefoxDriver driver = new FirefoxDriver();
driver.findElement(By.tagName("body")).sendKeys(Keys.CONTROL,"t");
Actions at=new Actions(wd);
at.moveToElement(we);
at.contextClick(we).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ENTER).build().perform();