简体   繁体   中英

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

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

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)?

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.

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)

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM