Java WebDriver 打开新标签页
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6421988/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WebDriver open new tab
提问by ChrisOdney
I have trawled the web and the WebDriver API. I don't see a way to open new tabs using WebDriver/Selenium2.0 .
我已经浏览了网络和 WebDriver API。我没有看到使用 WebDriver/Selenium2.0 打开新标签的方法。
Can someone please confirm if I am right?
有人可以确认我是否正确吗?
Thanks, Chris. P.S: The current alternative I see is to either load different urls in the same window or open new windows.
谢谢,克里斯。PS:我看到的当前替代方法是在同一窗口中加载不同的 url 或打开新窗口。
采纳答案by Jonathan Azoff
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):
使用 webdriver 完全有一种跨浏览器的方式来做到这一点,那些说你不能的人只是太懒了。首先,您需要使用 WebDriver 将标签注入并锚定到打开您想要的选项卡的页面中。这是我的做法(注意:驱动程序是一个 WebDriver 实例):
/**
* 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);
}
}
Next, you need to tell webdriver to switch its current window handle to the new tab. Here's how I do that:
接下来,您需要告诉 webdriver 将其当前窗口句柄切换到新选项卡。这是我如何做到的:
/**
* 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);
}
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 实例与新页面上下文中的元素进行交互。完成该选项卡后,您始终可以使用与上述 switchWindow 函数类似的机制返回到默认窗口上下文。我会把它留作练习,让你弄清楚。
回答by JimEvans
The Selenium WebDriver API does not support managing tabs within the browser at present.
Selenium WebDriver API 目前不支持在浏览器中管理标签页。
回答by Ola Ajibode
Though there is no API for opening a new tab, you can just create a new instance of WebDriver calling it something slightly different and passing the URL you want in the new tab. Once you have done all you need to do, close that tab and make the new driver NULL so that it does not interfere with the original instance of Webdriver. If you need both tabs open, then ensure you refer to the appropriate instance of WebDriver. Used this for Sitecore CMS automation and it worked.
虽然没有用于打开新选项卡的 API,但您可以创建一个 WebDriver 的新实例,调用它的内容略有不同,并在新选项卡中传递所需的 URL。完成所有需要做的事情后,关闭该选项卡并使新驱动程序为 NULL,这样它就不会干扰 Webdriver 的原始实例。如果您需要打开两个选项卡,请确保您引用了 WebDriver 的适当实例。将此用于 Sitecore CMS 自动化,并且有效。
回答by Mayur Shah
There's no way we can create new TAB or handle tabs using web driver / selenium 2.0
我们无法使用 Web 驱动程序 / selenium 2.0 创建新选项卡或处理选项卡
You can open a new window instead.
您可以改为打开一个新窗口。
回答by Pa_
I must say i tried this as well, and while it seemingly works with some bindings (Java, as far as Jonathan says, and ruby too, apparently), with others it doesnt: selenium python bindings report just one handle per window, even if containing multiple tabs
我必须说我也尝试过这个,虽然它似乎适用于一些绑定(Java,据乔纳森所说,而 ruby 显然也是如此),但对于其他人则没有:selenium python 绑定报告每个窗口只有一个句柄,即使包含多个标签
回答by bbbco
Thanks for the great idea @Jonathan Azoff !
感谢@Jonathan Azoff 的好主意!
Here's how I did it in Ruby:
这是我在 Ruby 中的做法:
def open_new_window(url)
a = @driver.execute_script("var d=document,a=d.createElement('a');a.target='_blank';a.href=arguments[0];a.innerHTML='.';d.body.appendChild(a);return a", url)
a.click
@driver.switch_to.window(@driver.window_handles.last)
end
回答by Shrikant Khadilkar
I would prefer opening a new window. Is there really a difference in opening a new window vs opening a new tab from an automated solution perspective ?
我宁愿打开一个新窗口。从自动化解决方案的角度来看,打开新窗口与打开新选项卡真的有区别吗?
you can modify the anchors target property and once clicked the target page would open in a new window.
您可以修改锚点目标属性,一旦单击目标页面将在新窗口中打开。
Then use driver.switchTo()
to switch to the new window. Use it to solve your issue
然后使用driver.switchTo()
切换到新窗口。用它来解决你的问题
回答by Anand
IJavaScriptExecutor is very useful class which can manipulate HTML DOM on run time through JavaScript, below is sample code on how to open a new browser tab in Selenium through IJavaScriptExecutor:
IJavaScriptExecutor 是一个非常有用的类,它可以在运行时通过 JavaScript 操作 HTML DOM,下面是有关如何通过 IJavaScriptExecutor 在 Selenium 中打开新浏览器选项卡的示例代码:
IJavaScriptExecutor js = (IJavaScriptExecutor)driver;
object linkObj = js.ExecuteScript("var link = document.createElement('a');link.target='_blank';link.href='http://www.gmail.com';link.innerHTML='Click Me';document.getElementById('social').appendChild(link);return link");
/*IWebElement link = (IWebElement)linkObj;
link.Click();*/
browser.Click("//*[@id='social']/a[3]");
Just to give an insight, there are no methods in Selenium which would allow you to open new tab, the above code would dynamically create an anchor element and directs it open an new tab.
仅供参考,Selenium 中没有允许您打开新选项卡的方法,上面的代码将动态创建一个锚元素并指示它打开一个新选项卡。
回答by deactivate
I had the same issue and found an answer. Give a try.
我遇到了同样的问题并找到了答案。试一下。
Robot r = new Robot();
r.keyPress(KeyEvent.VK_CONTROL);
r.keyPress(KeyEvent.VK_T);
r.keyRelease(KeyEvent.VK_CONTROL);
r.keyRelease(KeyEvent.VK_T);
It will open a new tab you can perform your actions in the new tab.
它将打开一个新选项卡,您可以在新选项卡中执行操作。
回答by user2283070
var windowHandles = webDriver.WindowHandles;
var script = string.Format("window.open('{0}', '_blank');", url);
scriptExecutor.ExecuteScript(script);
var newWindowHandles = webDriver.WindowHandles;
var openedWindowHandle = newWindowHandles.Except(windowHandles).Single();
webDriver.SwitchTo().Window(openedWindowHandle);