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.
What I use is the Robor class.
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 way I figure out for selenium 2, work fine for Chrome and firefox, IE has security check issue:
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.
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
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:
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
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.