Selenium 2: Open link in new tab and close tabs

前提是你 提交于 2019-11-27 20:14:40

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.

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

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!