Open a new tab in an existing browser session using Selenium
Asked Answered
I

6

22

My current code below in C# opens a window then navigates to the specified URL after a button click.

protected void onboardButton_Click(object sender, EventArgs e)
{
   IWebDriver driver = new ChromeDriver();
   driver.FindElement(By.CssSelector("body")).SendKeys(Keys.Control + "t");
   driver.Navigate().GoToUrl("http://www.google.com")
}

But the site that I am planning to navigate to has single sign-on. How can I open a new tab in my existing browser session and navigate from there? The above code does not seem to work.

Infield answered 11/1, 2017 at 9:51 Comment(1)
https://mcmap.net/q/587125/-open-link-in-new-tab-selenium-c also has a clearly explained solution.Tropine
M
25

To handle new tab you should switch to it first. Try following:

driver.FindElement(By.CssSelector("body")).SendKeys(Keys.Control + "t");
driver.SwitchTo().Window(driver.WindowHandles.Last());
driver.Navigate().GoToUrl("http://www.google.com")

Also you might need to switch back:

driver.SwitchTo().Window(driver.WindowHandles.First());
Mccaslin answered 11/1, 2017 at 9:54 Comment(12)
To clear things up, how should I initialize my driver? Should it be a global variable, right outside my buttonClick method? @MccaslinInfield
driver is an instance of your browser session object. If you want to simulate sequence of some actions except your onboardButton_Click() you should definitely define driver outside this function, globallyMccaslin
How can I initialize that existing session? I cannot use new ChromeDriver() because it would launch a new window instead of a new tab in the current session.Infield
You should initialize driver just once as global variable and then use it in your code. Sorry I'm not a C# coder, so I cannot tell much about how to write C# script... check this learnseleniumtesting.com/basic-webdriver-and-c-sharp for some tipsMccaslin
@Infield to make a global driver, make a static class and call it 'SeleniumInfo' or similar and inside it, add a property: "public static IWebDriver Driver { get; set; }" and call it like SeleniumInfo.DriverPlume
@Mccaslin @Happy Bird Do you guys have an idea on how this could be done in JavaScript? Instead of building a new window through chromedriver, the Selenium script would run on the existing browser instance instead. Thanks!Infield
@JPaulPunzalan, do you mean implementing the same in JavaScript + Selenium or just the command to pass into JavaScriptExecutor?Mccaslin
@Andersson, Is it possible for the driver to attach to an existing browser instance using JavaScript to bypass my firm's Single Sign-on? The workaround that we did before was through a button in a web application which was running on my parent chrome browser that has activated single sign-on. I am currently working on node scripts for browser automation.Infield
@JPaulPunzalan, it would be better to open new ticket with more details regarding this new issue as for now it's hard for me to understand what exactly you want to achieveMccaslin
@Andersson, let's say you have your chrome browser open at this moment (parent browser). Then you ran the selenium JS script via cmd. Right now, I cannot manipulate the chrome browser that is already open (parent browser). Every time I run the Selenium script via cmd, it instead opens a new browser using chromedriver.Infield
@JPaulPunzalan, oh... You mean you want to handle already opened (manually) browser with selenium? If so, AFAIK it's not possibleMccaslin
How would we open the new BrowserTurkish
G
32

Sending Keys.Control + "t" didn't work for me. I had to do it with javascript and then switch to it.

((IJavaScriptExecutor)driver).ExecuteScript("window.open();");
driver.SwitchTo().Window(driver.WindowHandles.Last());
Galibi answered 2/12, 2018 at 8:14 Comment(1)
the SendKeys(ctrl T) didn't work for me, but this one did. Plus I could put the URL in the window.open(url), and have it go to the correct page automatically, and not have to do a navigate after opening the tab.Hauck
M
25

To handle new tab you should switch to it first. Try following:

driver.FindElement(By.CssSelector("body")).SendKeys(Keys.Control + "t");
driver.SwitchTo().Window(driver.WindowHandles.Last());
driver.Navigate().GoToUrl("http://www.google.com")

Also you might need to switch back:

driver.SwitchTo().Window(driver.WindowHandles.First());
Mccaslin answered 11/1, 2017 at 9:54 Comment(12)
To clear things up, how should I initialize my driver? Should it be a global variable, right outside my buttonClick method? @MccaslinInfield
driver is an instance of your browser session object. If you want to simulate sequence of some actions except your onboardButton_Click() you should definitely define driver outside this function, globallyMccaslin
How can I initialize that existing session? I cannot use new ChromeDriver() because it would launch a new window instead of a new tab in the current session.Infield
You should initialize driver just once as global variable and then use it in your code. Sorry I'm not a C# coder, so I cannot tell much about how to write C# script... check this learnseleniumtesting.com/basic-webdriver-and-c-sharp for some tipsMccaslin
@Infield to make a global driver, make a static class and call it 'SeleniumInfo' or similar and inside it, add a property: "public static IWebDriver Driver { get; set; }" and call it like SeleniumInfo.DriverPlume
@Mccaslin @Happy Bird Do you guys have an idea on how this could be done in JavaScript? Instead of building a new window through chromedriver, the Selenium script would run on the existing browser instance instead. Thanks!Infield
@JPaulPunzalan, do you mean implementing the same in JavaScript + Selenium or just the command to pass into JavaScriptExecutor?Mccaslin
@Andersson, Is it possible for the driver to attach to an existing browser instance using JavaScript to bypass my firm's Single Sign-on? The workaround that we did before was through a button in a web application which was running on my parent chrome browser that has activated single sign-on. I am currently working on node scripts for browser automation.Infield
@JPaulPunzalan, it would be better to open new ticket with more details regarding this new issue as for now it's hard for me to understand what exactly you want to achieveMccaslin
@Andersson, let's say you have your chrome browser open at this moment (parent browser). Then you ran the selenium JS script via cmd. Right now, I cannot manipulate the chrome browser that is already open (parent browser). Every time I run the Selenium script via cmd, it instead opens a new browser using chromedriver.Infield
@JPaulPunzalan, oh... You mean you want to handle already opened (manually) browser with selenium? If so, AFAIK it's not possibleMccaslin
How would we open the new BrowserTurkish
L
6

The solution is really simple:

Driver.SwitchTo().NewWindow(WindowType.Tab);

Enjoy...

Ludeman answered 11/6, 2022 at 15:38 Comment(0)
D
2

This may not works:

driver.FindElement(By.CssSelector("body")).SendKeys(Keys.Control + "t");

Alternative: Find clickable element with target blank (search for "blank" in page's surce code). This will open new tab.

Than switch between tabs (thanks @Andersson) with:

driver.SwitchTo().Window(driver.WindowHandles.Last());
driver.SwitchTo().Window(driver.WindowHandles.First());
Determinable answered 3/4, 2018 at 20:42 Comment(0)
C
2

We can simulate Ctrl + Element Click

Actions action = new Actions(_driver);
action.KeyDown(Keys.Control).MoveToElement(body).Click().Perform();

Reference:
https://www.codeproject.com/Answers/1081051/Open-link-in-New-tab-using-Selenium-Csharp-for-chr#answer3

Continuum answered 19/11, 2019 at 1:37 Comment(0)
K
-5
IWebDriver driver = new ChromeDriver(); 

Change this to:

var driver = new ChromeDriver();

I do not know why. May be the IWebDriver miss the method.

Kettle answered 8/1, 2018 at 12:49 Comment(3)
How this could solve the issue!? OP need to switch to new window to be able to handle it. IWebDriver miss the method... Which method?Mccaslin
You might want to learn more about VAR so you can understand what is happening here :)Stewpan
ChromeDriver provides the method "Executescript". IwebDriver doesn't have that method...Please improve the answer..It is not wrong..Folse

© 2022 - 2024 — McMap. All rights reserved.