I am using:
- Chrome 55.0.2
- Selenium WebDriver 3.0.1 with ChromeDriver
- C# 4.6.1 (VS2015 Community Edition)
I am writing automation code that opens Chrome, opens a tab, and navigates to a main page URL. This main page has information that I parse to generate a secondary page URL. My issue is that I cannot write code that will switch back to the main page tab after the secondary page loads, nor can I write code that never leaves the main page. I will explain the two options I am pursuing:
Option #1 - behave like ctrl-click
After I generate this secondary page url, I would like to open another tab, have Chrome navigate to the url, but not leave the main page tab in Chrome. This is similar to ctrl-click
on a link.
Option #2 - behave like ctrl-1
(switch back to tab 1 in Chrome)
I would also be satisfied with a solution that after navigating and loading the secondary page url in a new Chrome tab, the automation will then switch back to the main page tab in Chrome. This is similar to typing ctrl-1
on a keyboard while Chrome has focus.
Option #3 - something else?
I would also be satisified with any other solution that accomplishes what I'm looking for (without introducing any new technology layer).
Some background information - Navigation
I tried the following approach first, but it fails my requirement because it opens the generated url in the same Chrome tab as the main page:
Session.Driver.Navigate().GoToUrl(generatedUrl);
I have not discovered a "direct" C#\Chrome\WebDriver command to open a url in a new tab in Chrome - it will always use the current tab.
I was able to find code using javascript that opens a url in a new tab. It looks like this:
// open a new tab and navigate to generated url:
IJavaScriptExecutor jscript = Session.Driver as IJavaScriptExecutor;
jscript.ExecuteScript(string.Format("window.open('{0}', '_blank');", generatedUrl));
And after executing the javascript, the secondary page is opened and is the active tab in Chrome. Here are my attempts to navigate back to the main page tab in Chrome:
My Attempts
I of course tried many ways to switch tabs, etc. Here they are, all failures:
Attempt #1: SwitchTo()
I am able to access each window handle using Session.Driver.WindowHandles
. When I debug the following command I see that the first window handle is indeed the main page; however, the command does nothing. No tab changes. Chrome keeps its focus on the secondary page\tab. Also, no error message. From what I've read online this command was supposed to switch the tabs.
Session.Driver.SwitchTo().Window(Session.Driver.WindowHandles.First());
Attempt #2: Send Keys - Action
This approach would be really great if it worked. Again, the following code has no impact and Chrome keeps it focus on the secondary page\tab. Also, no error message.
Actions action = new Actions(Session.Driver);
action.SendKeys(Keys.Control + "1").Build().Perform();
As a last ditch effort, I am currently searching for a JavaScript command that I can use with IJavaScriptExecutor
to switch tabs - no luck so far.
I'm providing you a trick using JavaScript to switching between tabs. This is not a perfect solution but you can use as alternate solution. You should try as :-
Note:- If you're getting alert exception when trying to accept alert and unable to find alert, You should wait until JavaScript trigger and alert box open.