4

I can easily do

driver.SwitchTo().Window(newTabInstance);

But I want to be able to handle multiple tabs at the same time without needing to switch to different tabs. Basically, being able to insert javascript into multiple tabs at the same time without needing to be on that tab. Is there a way to do it?

For example:

tab1.executejavascript("something");
tab2.executejavascript("something");
Mo D Genesis
  • 5,187
  • 1
  • 21
  • 32
  • 1
    Initially I was having the same doubt too and te answer was still the same.Though I was able to do the work in different windows (via threading) instead of different tabs. – Nimish Bansal Jan 06 '18 at 15:23
  • 1
    Yes that is the solution apparently, sad that we can't do the work in different tabs. – Mo D Genesis Jan 06 '18 at 18:39

4 Answers4

3

No, you won't be able to handle multiple tabs at the same time without switching to different tabs.


Reason

To perform any action Selenium needs focus. Unless the focus is on any particular TAB WebDriver won't be able to perform any action within that TAB/Window

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
0

Edit: Just like @DebanjanB said, selenium needs focus, so make a new class to handle the focus and focus back for you. The end result is exactly what you need, to run a script in multiple tabs with one command for each.

public class Tab
{
    private readonly string WindowIdentity;
    private readonly IWebDriver driver; //If you have a driver static class that can be accessed from anywhere,
    // then call the driver directly in the functions below, otherwise, initialize this variable in the constructor.

    /// <summary>
    /// Default constructor for Tab class, initializes the identity string from driver.
    /// </summary>
    /// <param name="windowIdentity">The unique string from running driver.</param>
    public Tab(string windowIdentity)
    {
        WindowIdentity = windowIdentity;
    }

    /// <summary>
    /// Runs the given script to the tab.
    /// </summary>
    /// <param name="script">The script to run.</param>
    public void RunScript(string script)
    {
        //Temporary variable to switch back to.
        string initialWindow = driver.CurrentWindowHandle;

        driver.SwitchTo().Window(WindowIdentity);
        (IJavaScriptExecutor)driver.ExecuteScript(script);
        driver.SwitchTo().Window(initialWindow);
    }
}

Whenever there is a new window, create a Tab object to manage it easier

//if the second entry of the array is your new tab
Tab tab1 = new Tab(driver.WindowHandles[1]) 

and then just call

tab1.RunScript("")'
0

Windows/tabs what's the difference. Just use multiple windows since windows in chrome can be joined to other windows and act as tabs. I think a work around should be devised by now since it is claimed that selenium needs "focus" to "focus" on one tab so you can't handle different tabs simultaneously yet you can open parallel windows and run them simultaneously and in a sense windows on these browsers are just tabs on their own which can be joined to form windows.

0

If changing focus is a problem because of interrupting other windows of softwares, then using Headless chrome is the solution. Hence, all the operations are performed in background and no interfere with other windows. However some milliseconds for changing tabs is inevitable.

ALalavi
  • 115
  • 1
  • 11