Ripon Al Wasim
Ripon Al Wasim

Reputation: 37826

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

Upvotes: 2

Views: 13412

Answers (5)

misiu_mp
misiu_mp

Reputation: 919

original_window = browser.current_window_handle
browser.switch_to.new_window('tab')
browser.get(some_url)
#do stuff
browser.close()
browser.switch_to.window(original_window) 

Upvotes: 0

Suresh
Suresh

Reputation: 26

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)

Upvotes: 1

Dhivya
Dhivya

Reputation: 699

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

Upvotes: 0

pap
pap

Reputation: 27604

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.

Upvotes: 1

hamon
hamon

Reputation: 1016

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

Upvotes: -1

Related Questions