How to create a new tab or window using Selenium Webdriver in Chrome browser
Asked Answered
C

3

2

I am trying to add a new tab using Webdriver 2.0 in Chrome but couldn't get the result.I have followed few answers provided in different forums.As I am very new to java and the answers available are more specific to Java scripting,I have posted this query to get a simple solution if possible.

E.g : The following statement is not triggering any action but the result in Selenium shows pass. Please advise. driver.findElement(By.tagName("body")).sendKeys(Keys.CONTROL +"t");

Christenechristening answered 27/7, 2013 at 15:29 Comment(2)
Do you mean you are trying to open a link in a new tab or add the functionality of opening the link in new tab to the html????Rudich
Selenium doesn't support tabs, and even if you are able to get a tab open, there is no guarantee on what behaviour will outcome from that. You may find the WebDriver just ignores it entirely, or you may find that you are able to interact with it. However, it's undefined behaviour of which you'll never get support for, ever, anywhere.Nilgai
C
2

When you can open a chrome window using

WebDriver driver = new ChromeDriver();

You can simply open a new window using

WebDriver driver2=new ChromeDriver();

You can access driver and driver2 in parallel or sequential.

To close a window do driver.close();

Clapper answered 28/7, 2013 at 14:47 Comment(0)
W
1

With the latest versions of selenium you can create a new tab, the line in the official github repo.
In C# is something like this:

ChromeDriver driver = new ChromeDriver();
driver.SwitchTo().NewWindow(WindowHint.Tab);

here is the junit test of java for creating a new tab

Wolframite answered 25/5, 2022 at 8:3 Comment(0)
D
0
    String URL = "https://www.instgram.com"; 
    ((JavascriptExecutor)driver).executeScript("window.open(arguments[0]),URL");

Selenium 4+ new feature to open new Tab or Window
 // Opens a new tab and switches to new tab
driver.switchTo().newWindow(WindowType.TAB);

// Opens a new window and switches to new window
driver.switchTo().newWindow(WindowType.WINDOW);
Dihybrid answered 5/3, 2023 at 8:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.