21

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.

Brian
  • 5,069
  • 7
  • 37
  • 47
JPaulPunzalan
  • 417
  • 1
  • 6
  • 20

6 Answers6

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());
nthpixel
  • 3,041
  • 3
  • 30
  • 42
  • 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. – AndyD273 Oct 02 '19 at 15:13
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());
Andersson
  • 51,635
  • 17
  • 77
  • 129
  • To clear things up, how should I initialize my driver? Should it be a global variable, right outside my buttonClick method? @Andersson – JPaulPunzalan Jan 11 '17 at 10:07
  • `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, globally – Andersson Jan 11 '17 at 10:13
  • 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. – JPaulPunzalan Jan 11 '17 at 10:51
  • 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 http://learnseleniumtesting.com/basic-webdriver-and-c-sharp for some tips – Andersson Jan 11 '17 at 10:55
  • @JPaulPunzalan 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.Driver – Happy Bird Jan 11 '17 at 16:00
  • @Andersson @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! – JPaulPunzalan Sep 26 '17 at 09:11
  • @JPaulPunzalan, do you mean implementing the same in `JavaScript` + `Selenium` or just the command to pass into `JavaScriptExecutor`? – Andersson Sep 30 '17 at 19:30
  • @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. – JPaulPunzalan Oct 01 '17 at 03:10
  • @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 achieve – Andersson Oct 01 '17 at 19:19
  • @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`. – JPaulPunzalan Oct 02 '17 at 01:30
  • @JPaulPunzalan, oh... You mean you want to handle already opened (manually) browser with selenium? If so, AFAIK it's not possible – Andersson Oct 02 '17 at 04:58
  • How would we open the new Browser – Ashok kumar Ganesan Apr 08 '20 at 09:13
5

The solution is really simple:

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

Enjoy...

MiMFa
  • 981
  • 11
  • 14
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());
Atanas Atanasov
  • 203
  • 1
  • 4
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

Wang Liang
  • 4,244
  • 6
  • 22
  • 45
-4
IWebDriver driver = new ChromeDriver(); 

Change this to:

var driver = new ChromeDriver();

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

Bucket
  • 7,415
  • 9
  • 35
  • 45
  • 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? – Andersson Mar 13 '18 at 13:28
  • You might want to learn more about VAR so you can understand what is happening here :) – user2455808 Oct 15 '20 at 05:07
  • ChromeDriver provides the method "Executescript". IwebDriver doesn't have that method...Please improve the answer..It is not wrong.. – Saurav Mar 23 '21 at 10:14