pythonseleniumselenium-webdriverwebdriverwindow-handles

Get the tab the user is viewing in selenium


Is there any way to determine which tab the user is viewing, like say the active tab (in selenium) is the last one and same for the user, then the user goes back to the first one, how do I know this with selenium?


Solution

  • From the information that has been gathered there seem to be no way to do this. What you can do is see if the current window is the active one using:

    driver.execute_script("return document.visibilityState") === "visible"
    

    But you cannot loop through the tabs and check this property since as soon as you change tab the tab you change to will also become the visible one.