Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to open a new tab using Selenium WebDriver in Java?

People also ask

Can Selenium open multiple tabs?

Just like you might open web pages in different tabs locally, it's also possible to have multiple tabs up in one browser during a Selenium test.

How do I open multiple browsers in Selenium WebDriver?

Open Multiple Tabs Use One WebDriver Object. Send "Ctrl+t" command to body element to open a new browser tab. Send "Ctrl+2" command to navigate to the second browser tab. Change the URL to google.com for the second browser tab. Get the body web element in the second browser tab.

How do I open a page in Selenium?

Open Webpage in selenium We can open the browser by using get("") a non-static method present in the FirefoxDriver class, it accepts a string as arguments, and the string should be the website address. A special thing about this get("") method is it will not give control to the next line till the page loads completely.


Just for anyone else who's looking for an answer in Ruby, Python, and C# bindings (Selenium 2.33.0).

Note that the actual keys to send depend on your OS. For example, Mac uses CMD + T, instead of Ctrl + T.

Ruby

require 'selenium-webdriver'

driver = Selenium::WebDriver.for :firefox
driver.get('http://stackoverflow.com/')

body = driver.find_element(:tag_name => 'body')
body.send_keys(:control, 't')

driver.quit

Python

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

driver = webdriver.Firefox()
driver.get("http://stackoverflow.com/")

body = driver.find_element_by_tag_name("body")
body.send_keys(Keys.CONTROL + 't')

driver.close()

C#

using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;

namespace StackOverflowTests {

    class OpenNewTab {

        static void Main(string[] args) {

            IWebDriver driver = new FirefoxDriver();
            driver.Navigate().GoToUrl("http://stackoverflow.com/");

            IWebElement body = driver.FindElement(By.TagName("body"));
            body.SendKeys(Keys.Control + 't');

            driver.Quit();
        }
    }
}

The code below will open the link in a new tab.

String selectLinkOpeninNewTab = Keys.chord(Keys.CONTROL,Keys.RETURN);
driver.findElement(By.linkText("urlLink")).sendKeys(selectLinkOpeninNewTab);

The code below will open an empty new tab.

String selectLinkOpeninNewTab = Keys.chord(Keys.CONTROL,"t");
driver.findElement(By.linkText("urlLink")).sendKeys(selectLinkOpeninNewTab);

Do this:

driver.ExecuteScript("window.open('your URL', '_blank');");

To open new tab using JavascriptExecutor,

((JavascriptExecutor) driver).executeScript("window.open()");
ArrayList<String> tabs = new ArrayList<String>(driver.getWindowHandles());
driver.switchTo().window(tabs.get(1));

Will control on tab as according to index:

driver.switchTo().window(tabs.get(1));

Driver control on main tab:

driver.switchTo().window(tabs.get(0));

You can use the following code using Java with Selenium WebDriver:

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

By using JavaScript:

WebDriver driver = new FirefoxDriver(); // Firefox or any other Driver
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("window.open()");

After opening a new tab it needs to switch to that tab:

ArrayList<String> tabs = new ArrayList<String>(driver.getWindowHandles());
driver.switchTo().window(tabs.get(1));

To open a new window in Chrome Driver.

// The script that will will open a new blank window
// If you want to open a link new tab, replace 'about:blank' with a link
String a = "window.open('about:blank','_blank');";
((JavascriptExecutor)driver).executeScript(a);

For switching between tabs, read here.