如何使用带有 Java 的 Selenium WebDriver 在同一浏览器中打开一个新选项卡?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12139173/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 07:44:54  来源:igfitidea点击:

How to open a new tab in the same browser by using Selenium WebDriver with Java?

javaselenium-webdriver

提问by Ripon Al Wasim

I can open a new window with Selenium WebDriver by using Java and JavaScript. I am using Firefox. The code is as follows:

我可以使用 Java 和 JavaScript 打开带有 Selenium WebDriver 的新窗口。我正在使用 Firefox。代码如下:

WebDriver driver = new FirefoxDriver();
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("function createDoc(){var w = window.open(); w.document.open(); w.document.write('<h1>Hello World!</h1>'); w.document.close();}; createDoc();");

How can I open a new tab in the same browser by using WebDriver (Selenium 2)?

如何使用 WebDriver (Selenium 2) 在同一浏览器中打开新标签页?

采纳答案by Suresh

Wasim,

瓦西姆,

cdriver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL +"t");

You can use above line to open new tab in a same browser (Works in Firefox)

您可以使用上面的行在同一浏览器中打开新选项卡(适用于 Firefox)

回答by pap

There is no standard support in JavaScript or HTML for opening a link in a tab vs a window. It's depending on browser and settings. Some browsers default to opening in new tabs (like Chrome and Safari). Some browser allows the user to configure the behavior. Bottom line, you shouldn't design your site to rely on opening new windows in tabs as there is no reliable and cross-browser compatible mechanism for doing that.

JavaScript 或 HTML 中没有标准支持在选项卡和窗口中打开链接。这取决于浏览器和设置。某些浏览器默认在新标签页中打开(如 Chrome 和 Safari)。某些浏览器允许用户配置行为。最重要的是,您不应将站点设计为依赖于在选项卡中打开新窗口,因为没有可靠且跨浏览器兼容的机制可以做到这一点。

回答by Dhivya

It is mostly depends on your browser settings and there is no separate methods for opening link in new window or new tab

这主要取决于您的浏览器设置,并且没有单独的方法可以在新窗口或新选项卡中打开链接

回答by hamon

It also depends on how your browser is configured to open pop-up's.

它还取决于您的浏览器如何配置为打开弹出窗口。