I am using Selenium 2.52.0 in Java and Firefox 44.0.2. Unfortunately, none of the above solutions worked for me. The problem is that if I call driver.getWindowHandles (), I always get one single descriptor. Somehow it makes sense to me, since Firefox is the only process, and each tab is not a separate process. But maybe I'm wrong. In any case, I'm trying 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 the combination Ctrl + t to open a new tab, Ctrl + w to close it, and to return to the original tab, I used Ctrl + 1 (first tab). I know that my solution is not perfect or even good, and I would also like to switch using switch switch to call, but as I wrote, this was not possible, since I had only one descriptor. Perhaps this will be useful for someone with the same situation.
Gico Mar 05 '16 at 22:02 2016-03-05 22:02
source share