简体   繁体   中英

Selenium 2: Open link in new tab and close tabs

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.

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

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

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.

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

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)

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.

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