Neo The Hacker
Neo The Hacker

Reputation: 67

How to create diffrent sessions(tabs) in one browser and duplicating tab using IInputSimulator

I am using ChromeDriver with Selenium in my WPF desktop(.net core) application and I want to open different tabs(Sessions) in 1 Chrome browser For that I am using IInputSimulator(virtual keyboard) with the shortcut Ctrl+Shift+x but it is not opening as per the given TabCount.

Do you have any solution or alternate way to achieve the same?

Here is my code snippet:

for (var i = 0; i < TabCount - 1; i++)
{
                try
                {
                    _logger.Info($"Tab changed: i={i}, tab count:{TabCount}");
                    _driver.SwitchTo().Window(mainHandler);

                    Thread.Sleep(2500);
                    _inputSimulator.Keyboard.ModifiedKeyStroke(new[] { VirtualKeyCode.CONTROL, VirtualKeyCode.SHIFT }, new[] { VirtualKeyCode.VK_X });

                    _driver.SwitchTo().Window(_driver.WindowHandles.Last());
                    if (_driver.Url != Constant.TabUrls.Google_URL)
                    {
                        _driver.Navigate().GoToUrl(Constant.TabUrls.Google_URL);
                        Thread.Sleep(2500);
                    }
                }
                catch (Exception ex)
                {
                    _logger.Error(ex.Message, ex);
                }
                _logger.Info("Go back to main handler");
}

Upvotes: 0

Views: 160

Answers (2)

Neo The Hacker
Neo The Hacker

Reputation: 67

After some R&D I found a good extension for Chrome which is "SessionBox - Multi login to any website" Session Box

However, it is paid you can check the pricing here: https://sessionbox.io/pricing

Again you can not perform anything on the extension/popup above the bookmark bar and you can operate inside the browser screen only using Selenium... right?

So for that, I have opened the extension URL which is the same UI available on a web page as appears on the extension popup.

The link can be something like this after installing the extension: chrome-extension://megbklhjamjbcafknkgmokldgolkdfig/views.html?view=onBoarding

For that here is my core logic applied on the extension URL page:

_driver.SwitchTo().Window(handler);
_driver.Navigate().GoToUrl(Constant.TabUrls.FACEBOOK_URL);

//open sessionbox extension URL in seprate window
string sessionBoxExt = _driver.SwitchTo().NewWindow(WindowType.Window).CurrentWindowHandle;
_driver.Navigate().GoToUrl(Constant.TabUrls.SESSION_BOX.Split('?')[0]);
Thread.Sleep(200);

_driver.SwitchTo().Window(sessionBoxExt);
Thread.Sleep(2000);

//Go to main handler and create stored session using simulator
_driver.SwitchTo().Window(handler);
Thread.Sleep(200);
_inputSimulator.Keyboard.ModifiedKeyStroke(new[] { VirtualKeyCode.CONTROL, VirtualKeyCode.SHIFT }, new[] { VirtualKeyCode.VK_S });
Thread.Sleep(200);

//Go to sessionbox extension
_driver.SwitchTo().Window(sessionBoxExt);

//Click to create duplicate multiple session icon button
Thread.Sleep(200);
"//div//button[contains(@class,'mainClone')]".FindAndClickElement(ElementFindByEnum.XPath, _wait, _action);

_driver.SwitchTo().Window(handler);
Thread.Sleep(200);

try
{
    // Pass the tabCount to propmt to open multipe duplicate sessions.
    if (_wait.Until(ExpectedConditions.AlertIsPresent()) != null)
    {
        IAlert alert = _driver.SwitchTo().Alert();

        if (alert != null)
        {
            alert.SendKeys(tabCount.ToString());
            alert.Accept();
            _logger.Info($"{tabCount} new sessions created.");
            Thread.Sleep(10000);
        }
    }
}
catch (WebDriverTimeoutException ex)
{
    _logger.Error(ex.Message, ex);
}

_driver.SwitchTo().Window(sessionBoxExt);
_driver.Close();

Upvotes: 0

ioi
ioi

Reputation: 41

According to tab-window-shortcuts, there is no shortcuts for tab duplicating. Use the following instead:

driver.SwitchTo().NewWindow(WindowType.Tab);
driver.Navigate().GoToUrl(targetUrl);

Upvotes: 0

Related Questions