Selenium 2: Open link in new tab and close tabs
Asked Answered
E

6

13

I want to be able to open a link in a new tab in Selenium 2. Also i want to close the tab when i am finished interacting with the page. How is this possible if I have a WebElement of an <a> tag?

I am using the Java API of Selenium 2 with the Firefox Driver, running on Firefox 4.

Earleneearley answered 17/5, 2011 at 14:21 Comment(3)
I thought of a solution using JavaScript. Maybe that's the easiest way?Earleneearley
Would you share this solution? I'm having the same problem.Incumbent
I did not found a solution. It seems that there is no way to do this, see also the accepted answer.Earleneearley
A
6

At the moment, the Selenium WebDriver API doesn't have any way of handling tabs. The project would really need a consistent, cross-browser set of methods for managing tabs before I would expect to see an implementation in one of the language bindings like Java. Until then, your JavaScript solution may be the only way, and remember that your code would then be responsible for managing the lifetime of that tab.

Ashkhabad answered 17/5, 2011 at 19:17 Comment(1)
I was afraid that this would be the answer. Thank you.Earleneearley
N
7

The way I figure out for selenium 2, work fine for Chrome and firefox, IE has security check issue:

Set<String> winSet = webDriver.getWindowHandles();
List<String> winList = new ArrayList<String>(winSet);
String newTab = winList.get(winList.size() - 1);
webDriver.close(); // close the original tab
webDriver.switchTo().window(newTab); // switch to new tab
Nancienancy answered 24/4, 2012 at 20:9 Comment(1)
as far as i understood, this method only allows handling of windows, not tabsEarleneearley
A
6

At the moment, the Selenium WebDriver API doesn't have any way of handling tabs. The project would really need a consistent, cross-browser set of methods for managing tabs before I would expect to see an implementation in one of the language bindings like Java. Until then, your JavaScript solution may be the only way, and remember that your code would then be responsible for managing the lifetime of that tab.

Ashkhabad answered 17/5, 2011 at 19:17 Comment(1)
I was afraid that this would be the answer. Thank you.Earleneearley
P
6

to use selenium at its best we at sol-logics combine it with java.awt.robot class. you can send keys that can close a browser window. try using

Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_W);

and reply if it works

Planking answered 19/12, 2012 at 10:43 Comment(0)
S
4

Took awhile (~2 weeks) for me to track down the right sequence of commands, but this is by far the easiest method I've found for a Win7/Chrome setup to open a link in a new tab AND switch to the new tab automatically.

WARNING! Make sure to always perform the keyUp actions. If you fail to perform keyUp your system will keep those keys pressed until a reboot or keyUp occurs.

Windows 7/Chrome:

WebElement elem = driver.findElement(By.linkText("MyLinkText"));

// Chrome key combos:
//   SHIFT + CTRL + click = Open in new tab (and switch to new tab)
//   SHIFT + CTRL + RETURN = Open in new tab (in background)
Actions act = new Actions(driver);
act.keyDown(Keys.LEFT_CONTROL).keyDown(Keys.LEFT_SHIFT).perform();

// Wrap in a try/catch during implementation to ensure you perform keyUp(s).
elem.click();

act.keyUp(Keys.LEFT_CONTROL).keyDown(Keys.LEFT_SHIFT).perform();

Note: I know it's an old thread, I just wanted to catalog the solution here because I couldn't find a more elegant solution and wanted to save someone else a little time (hopefully :).

Edit: Typo

Stereography answered 12/1, 2015 at 18:16 Comment(1)
Wish I could have helped w/ FF specifically but unfortunately we use Chrome as our primary test browser. However the same general technique should work for others w/ their respective key combos.Stereography
C
3

Here is how i did it using Python.

This solution is a bit dirty but it works if you want to close the tab.

Im mimicking the mac shortcut CMD + W to close a tab, if you are running windows you may have to implement a different key combination.

import from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
from selenium import webdriver

driver = webdriver.Firefox()
driver.get("http://www.amazon.com/gp/search/ref=sr_in_-2_p_lbr_brands_browse-_2895?rh=n%3A172282%2Cn%3A!493964%2Cn%3A502394%2Cp_lbr_brands_browse-bin%3ALytro")
action_chains = ActionChains(driver)
action_chains.key_down(Keys.COMMAND + "w")
action_chains.perform()
action_chains.key_up(Keys.COMMAND + "w")
driver.implicitly_wait(5)
Chromomere answered 6/12, 2013 at 6:41 Comment(0)
A
3

What I use is the Robor class.

Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_W);
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.keyRelease(KeyEvent.VK_W);

This makes the Robot rapidly press and release the CTRL + W keys to simulate a user interaction. If you only use keyPress event, this will close all the tabs and windows of the WebDriver.

Hope I helped you.

Audryaudrye answered 5/7, 2014 at 4:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.