Selenium 2:在新标签页中打开链接并关闭标签页

Alp*_*Alp 13 java webdriver firefox4 browser-tab selenium-webdriver

我希望能够在Selenium 2中的新选项卡中打开链接.此外,我想在完成与页面的交互时关闭选项卡.这怎么可能,如果我有WebElement一个的<a>标签?

我正在使用Selenium 2的Java API和Firefox 4上运行的Firefox驱动程序.

小智 7

我找到selenium 2的方法,适用于Chrome和firefox,IE有安全检查问题:

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
Run Code Online (Sandbox Code Playgroud)

  • 据我所知,这种方法只允许处理`windows`,而不是`tabs` (2认同)

Jim*_*ans 6

目前,Selenium WebDriver API没有任何处理标签的方法.在我期望看到Java之类的语言绑定中的实现之前,该项目确实需要一组用于管理选项卡的一致的跨浏览器方法.在此之前,您的JavaScript解决方案可能是唯一的方法,并记住您的代码将负责管理该选项卡的生命周期.


小智 6

最好使用selenium我们在sol-logics中将它与java.awt.robot类结合起来.您可以发送可以关闭浏览器窗口的密钥.尝试使用

Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_W);
Run Code Online (Sandbox Code Playgroud)

并回复它是否有效