With Selenium, a URL can be opened in a new tab. For this, you can apply the sendKeys and Keys.chord methods. Multiple keys can be provided at once using the Keys.chord function. We can also launch the new tabs in the same browser using Selenium WebDriver and switch between them. This article discusses about the Selenium WebDriver’s new tab-opening process and functionality. We demonstrate this function with a few simple examples.
How to Open a New Tab in Selenium
Using Selenium, it is possible to automate the browser instructions. It is functional for all programmers, works with all leading operating systems, and has content created in several languages including Python, Java, C#, and others. The new window command from Selenium 4 makes it easier for its users to open a new tab or window. The created tab or window has the focus after the command is executed.
Command to Open a New Tab in Selenium
The following command can be used to open a new tab in Selenium:
It can also be written as follows:
driver.get(URL);
Now, let’s explain some examples which explain the process of opening a new tab in Selenium.
Example 1:
In this first example, we use some simple and basic methods for inexperienced and experienced developers to help them in their initial coding.
To execute the code, we need to create a class named “NewTabExample” first.
Now, we execute the following code:
{
public static void main(String[] args)
{
WebDriverManager.chromedriver().setup();
WebDriver mydriver = new ChromeDriver();
mydriver.manage().timeouts().implicitlyWait(3O, TimeUnit.SECONDS);
mydriver.get("http://www.bing.com");
System.out.println(mydriver.getTitle());
mydriver.switchTo().newWindow(WindowType.TAB);
Now, we understand this code. First, we launch the “bing.com” through the driver’s “.get” method. Then, we add a command print in the driver’s “.getTitle” which gives you the title of the page. We write the driver “.switch” to the new window method. In this particular new window, you can pass either a new tab or new window that you want to open. We write the kind of window type. So, write the window name. The window type is an enum (which is either you want to open a new tab or new window). Here, we open a new tab so we write “tab”. After running this code, it opens a new blank tab. Now, let’s run it and see the result.
After running the previous code, we get the result in two windows: one is bing.com and the other tab is blank.
After opening a new tab, it shows where exactly the driver is available. For example, we open two tabs – tab number 1 and tab number 2. In tab number 1, we have “bing.com”. In tab number 2, we have “facebook.com”. Earlier, when we launch our driver where we launch “bing.com”, the driver is available in the first tab. But the moment we write the “driver.getTitle”, that is bing. The moment we write the “driver.switch” to a new window, the window type is tab. Now, the new blank window is opened. The driver switches over here which means that the driver is available in the open new tab. So, the driver control is available over here.
After that, if we write the printing “driver.getTitle” again, the driver is available on the blank window in the blank tab. It should not give you any title again. When you run the code, you will find a blank tab.
Example 2:
Now, we first create a new class to perform the scripting called “NewWindowExample”.
The first step to open the Selenium is to input the setup.setproperty and the chrome driver. Then, input the packages for both of them.
WebDriver mydriver = new ChromeDriver();
mydriver.manage().window().maximize();
To achieve our goal, write the URL driver.get() first and paste the specific URL in it. Now, when we run the script, we get the specific window in a new tab.
If we want to deal with multiple windows, we switch from one window to another. To execute that, we write a “driver.Switch”. Then, we can see that there are options which are open in Selenium. The options include the following:
-
- switchTo.frame(int frameNumber)
- switchTo.frame(WebElement frameElement)
- switchTo.frame(string frameName)
- switchTo().defaultContent()
We examine this whole procedure through our coding.
System.setProperty(“webdriver.chrome.driver”, “C:\\Users\\abc\\Downloads\\chromedriver_win32\\chromedriver.exe");
WebDriver mydriver = new ChromeDriver();
mydriver.manage().window().maximize();
mydriver.get(“https://www.ebay.com”)
When we run this code in Selenium, we get the Amazon page as an output in our new tab.
Using this particular line (given previously), we convince Selenium to open a brand-new tab when the execution is in place.
Now, we need to add another URL. What does our script do after running the code? First, the Amazon window opens. Then, the sales force window opens.
mydriver.get(“https://bing.com”)
Example 3:
Now, in the third and last example of this article, we create a new class name and create a new window or tab. In this code, we use the WebDriver and specify a variable. Then, using a WebDriver, we can create an object using the web manager. This opens as a Chrome driver. Now, we need to open the URL. For that, we use the get method which is present in the WebDriver. We need to provide the URL and open it. So, let’s give that specific URL and create a string variable called URL and the driver.find element, By Xpath locator that we found. This retruns the WebElement. Then, input the “print.out.print.in” since we aim to open this URL in a new tab or a new window. We can achieve that using the “driver.switch” to the new window method which is present in Selenium. We need to provide the type of window to the “new window type” like what it should be, whether it’s a tab or a window. But the window type is nothing but an enum.
WINDOW (“window”),
TAB (“tab”),
Enum accepts either a window or a tab. We need to provide that to the “window.type.tab” method. See the rest of the code in the following:
driver.manage().window().maximize();
String urlname = driver.findElement(By.xpath("//table[@id=’table1’/tbody/ System.out.println(ur1name);
mydriver.switchTo().newWindow(WindowType.TAB);
mydriver.get(ur1name);
System.out.println(mydriver.getTitle());
This opens in a new tab and switches to that tab. Once the tab is open, we can open our URL whenever we use the driver.get. The URL, which is the string this way, opens in a new tab.
Conclusion
We provided a brief introduction to the Selenium WebDriver and discovered how to open a new tab in Selenium in detail. With the help of some examples, we also learned how to open multiple tabs in the Selenium WeDriver and some of the functions of Selenium.