2

I'm creating app which is half-automated (user is opening tabs (attention) and if he wants to dump one of them he just clicks hot-key).

But when user opens to much tabs, I need to know to which one I should switch. How can i get currenttab index. Or switch to current tab on Selenium C#?

string windowHandle = Browser.WindowHandles.Last();
string windowHandle = Browser.WindowHandles.First();
string windowHandle = Browser.WindowHandles[1];

... is not working for me.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
GrandMaster
  • 31
  • 1
  • 3
  • I want to know in which tab are user. Remind you, user can open and close tabs on chrome. – GrandMaster Aug 17 '19 at 01:09
  • Not possible in half automated app using Webdriver protocol.Webdriver client need to keep track of active tab . Selenium will not be able to detect the user action on its own as there is no reverse communication in case of any action in browser by user. Webdriver client only parse the response of the request generated by client i.e. c# binding in your case.You can check Selenium architecture for better understanding. – Rahul L Aug 18 '19 at 04:47
  • Which extension do you recommend? I can re-create the project. Which way to go? Preferably on C#. thnks. – GrandMaster Aug 21 '19 at 20:41
  • I want to say only one thing - I work only with the active tab. How can I do this, even without selenium. – GrandMaster Aug 21 '19 at 22:07

1 Answers1

3

The currenttab index may get changed everytime you invoke Browser.WindowHandles().

Though the general perception is WindowHandles would be sorted like the oldest windows first and the newest windows last. But this is not the case: It is totaly random !

In a discussion, Simon clearly mentioned:

While the datatype used for storing the list of handles may be ordered by insertion, the order in which the WebDriver implementation iterates over the window handles to insert them has no requirement to be stable. The ordering is arbitrary.

This comment is pretty much inline with the Get Window Handles section where it mentioned:

In order to determine whether or not a particular interaction with the browser opens a new window, one can obtain the set of window handles before the interaction is performed and compare it with the set after the action is performed.

You can find a relevant detailed discussion in Best way to keep track and iterate through tabs and windows using WindowHandles using Selenium


Update

As per your comment user switch tab (in window) but driver is still focused on another tab you need to induce WebDriverWait for numberOfWindowsToBe(n) and you can find a detailed discussion in getWindowHandles() not working in firefox 58.The focus remains on parent tab and does not transfer to next tab

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • My program is half-automated. User, if they want, click a button on my program and program do things on that tab of google chrome. How can i know which tab is it. Tabs are to much if user want. – GrandMaster Aug 18 '19 at 01:28
  • The problem is that user switch tab (in window) but driver is still focused on another tab. I need to detect active tab to be able switch driver to it. – GrandMaster Aug 18 '19 at 01:49
  • @GrandMaster Checkout the answer update and let me know the status. – undetected Selenium Aug 19 '19 at 07:42
  • I know that you want to help, but that’s not what I need. As I said, the program is half-automatic. The user can open many tabs (it is not known how many). And being in one of them, user can click on the application button. Selenium should be focused on this tab to work with it. I already understand that this cannot be achieved by the Selenium. – GrandMaster Aug 22 '19 at 21:34