1

I am trying to get the index or window id of currently visiting page not the active tab.

I am trying to access a specific tab in multiple tabs opened by bot I want to know that if user clicked on the unfocused tab e.g. tab no 3 but the active/focus tab is tab no 6. I want to shift the focus to user visiting page.

Sample code of mine which is not working.

for i in web.window_handles:
    if not web.execute_script("return document.visibilityState") == "visible":
        web.switch_to.window(i)
        current_opened_window = web.current_window_handle

    else:
        current_opened_window = i

Sample Picture

Abdul Moez
  • 42
  • 6
  • What do you mean by "index of user"? And what does "not working" mean? What happens when you run your code? What do you want it to do instead? Please [edit] your question to add more details so we can understand what you are trying to do. – Code-Apprentice Jul 26 '22 at 19:27
  • I am trying to access a specific tab in multiple tabs opened by bot I want to know that if user clicked on the unfocused tab e.g. tab no 3 but the active/focus tab is tab no 6. I want to shift the focus to user visiting page. – Abdul Moez Jul 26 '22 at 19:33

1 Answers1

0

Presumably Selenium can focus only on a specific tab at a time and to shift the focus to other tabs you need tab handling.

Hence, any click attempt on a WebElement will try to locate the element on the focused tab/page first. Incase the element isn't found NoSuchElementException would be raised.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • I know window handles. But they are not helping. Suppose that bot opened 3 tabs and working on last handle (tab3) but user clicked on handle 2 (tab2) the bot will still working on tab3 because we cannot detect that which page/tab is visited by user – Abdul Moez Jul 27 '22 at 03:07
  • `but user clicked on handle 2 (tab2)`(manual step) -> you need to shift selenium's focus to tab2 – undetected Selenium Jul 27 '22 at 04:54
  • `the bot will still working on tab3` -> correct, (unless you window handle and change selenium's focus) bot will try to locate the element in tab3 only. – undetected Selenium Jul 27 '22 at 04:57
  • As this question if user opened 50 tabs I can get then by window handles but if user switch to any other tab in 50 tabs how can I know which tab he goes on and switch the focus to that tab. like tab indexing [link](https://stackoverflow.com/questions/57532534/how-to-get-currently-active-tab-index-on-chrome-via-selenium) – Abdul Moez Jul 27 '22 at 05:04
  • Selenium won't know which tab you are in but still keep on trying to execute the actions on the tab currently it is having focus, you have to change Selenium's focus to the desired tab to perform automated action, ex: click an element. Period. – undetected Selenium Jul 27 '22 at 05:06
  • Yes, selenium does not detect it. I want to know the index of that tab with any third source like google extension (query) but not working or with java script. I don't know. – Abdul Moez Jul 27 '22 at 05:09
  • _index of that tab_: Which index are you speaking about? – undetected Selenium Jul 27 '22 at 05:13
  • like index of first tab is 0 and second 1 and so on. And if user is not visiting the tab we get the visiting tab index and switch to it by window handles. – Abdul Moez Jul 27 '22 at 05:20
  • _index of first tab is 0 and second 1 and so on_: is quite hypothetical. I won't even attempt to open a third tab unless I close the second one. – undetected Selenium Jul 27 '22 at 05:24
  • We don't do but it is my client requirement. – Abdul Moez Jul 27 '22 at 06:25
  • Being a SDET/Test Architect you need to fix that logically convincing the stakeholders :) – undetected Selenium Jul 27 '22 at 06:27