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

2
  • 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. Commented Jul 26, 2022 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. Commented Jul 26, 2022 at 19:33

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

11 Comments

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
but user clicked on handle 2 (tab2)(manual step) -> you need to shift selenium's focus to tab2
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.
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
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.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.