Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 674
Alp Avatar asked May 17 '11 14:05

Alp


2 Answers

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
like image 78
Henry Avatar answered Oct 04 '22 20:10

Henry


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.

like image 45
JimEvans Avatar answered Oct 04 '22 19:10

JimEvans