打开的webdriver新标签 [英] WebDriver open new tab

查看:190
本文介绍了打开的webdriver新标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经搜罗网络和API的webdriver。我不明白的方式打开使用的webdriver / Selenium2.0新的选项卡。

I have trawled the web and the WebDriver API. I don't see a way to open new tabs using WebDriver/Selenium2.0 .

有人可以确认,如果我是正确的?

Can someone please confirm if I am right?

谢谢,
克里斯。
P.S:当前备选我看到的是在同一个窗口要么加载不同的URL或打开新窗口

Thanks, Chris. P.S: The current alternative I see is to either load different urls in the same window or open new windows.

推荐答案

有完全是一个跨浏览器的方式做到这一点使用webdriver的,那些谁说你不能只是太懒惰。首先,你需要使用webdriver的注入和锚标记为打开所需的选项卡页面。以下是我如何做到这一点(注:驱动程序是一个webdriver的实例):

There is totally a cross-browser way to do this using webdriver, those who say you can not are just too lazy. First, you need to use WebDriver to inject and anchor tag into the page that opens the tab you want. Here's how I do it (note: driver is a WebDriver instance):

/**
 * Executes a script on an element
 * @note Really should only be used when the web driver is sucking at exposing
 * functionality natively
 * @param script The script to execute
 * @param element The target of the script, referenced as arguments[0]
 */
public void trigger(String script, WebElement element) {
    ((JavascriptExecutor)driver).executeScript(script, element);
}

/** Executes a script
 * @note Really should only be used when the web driver is sucking at exposing
 * functionality natively
 * @param script The script to execute
 */
public Object trigger(String script) {
    return ((JavascriptExecutor)driver).executeScript(script);
}

/**
 * Opens a new tab for the given URL
 * @param url The URL to 
 * @throws JavaScriptException If unable to open tab
 */
public void openTab(String url) {
    String script = "var d=document,a=d.createElement('a');a.target='_blank';a.href='%s';a.innerHTML='.';d.body.appendChild(a);return a";
    Object element = trigger(String.format(script, url));
    if (element instanceof WebElement) {
        WebElement anchor = (WebElement) element; anchor.click();
        trigger("var a=arguments[0];a.parentNode.removeChild(a);", anchor);
    } else {
        throw new JavaScriptException(element, "Unable to open tab", 1);
    }       
}

接下来,你需要告诉的webdriver到目前的窗口句柄切换到新的选项卡。下面是我如何做到这一点:

Next, you need to tell webdriver to switch its current window handle to the new tab. Here's how I do that:

/**
 * Switches to the non-current window
 */
public void switchWindow() throws NoSuchWindowException, NoSuchWindowException {
    Set<String> handles = driver.getWindowHandles();
    String current = driver.getWindowHandle();
    handles.remove(current);
    String newTab = handles.iterator().next();
    locator.window(newTab);
}

此过程完成后,你便可以使用相同的webdriver实例在新的页面上下文元素进行交互。一旦你与该选项卡中完成,你随时可以通过类似的机制,以上述switchWindow函数返回到默认的窗口背景。我会离开,作为一个练习,你要弄清楚。

After this is done, you may then interact with elements in the new page context using the same WebDriver instance. Once you are done with that tab, you can always return back to the default window context by using a similar mechanism to the switchWindow function above. I'll leave that as an exercise for you to figure out.

这篇关于打开的webdriver新标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆