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

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

问题描述

我已经搜索了网络和 WebDriver API.我看不到使用 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天全站免登陆