Search code examples
javaselenium-webdriverwebdriver

How to get the visible tab in Selenium (Java)?


Note: I have already searched many posts, some even asking this same question, but I could not find any answers

How can I get the current visible/active tab? The closest I could get was

driver.executeScript("return document.visibilityState")

but looping over and switching the windows would result in this always printing visible.

Both driver.getWindowHandle() and driver.getCurrentUrl() return a different (random?) tab, not the one that the user has open.

Main goal:

  1. User opens browser
  2. User opens multiple tabs and browses normally
  3. User starts my application (connecting to the browser using the remote debugging port)
  4. The application is able to detect what tab the user is currently browsing on (this step doesn't work!)

Is this possible?

Or, is there an alternative other than Selenium which can achieve this same goal? Preferably in Java or Kotlin, but I guess I might have to learn how to make browser extensions... (but trying to see if it is possible without making an extension)


Solution

  • Note: Tested with Microsoft Edge

    I decided not to use Selenium, and instead sent a GET request to http://localhost:9222/json. After filtering the list out into only the tabs that have the type page, I connected a WebSocket to ws://127.0.0.1:9222/devtools/page/TAB_ID (replacing TAB_ID with the id from the GET request above) for each tab. Then this allows you to run JavaScript on each tab (even if it's not shown!) using the Runtime.evaluate method (this JSON should be sent through the WebSocket connection):

    {
        "id": 1,
        "method": "Runtime.evaluate",
        "params": {
            "expression": "!document.hidden"
        }
    }
    

    which will return a result like:

    {
        "id": 1,
        "result": {
            "result": {
                "type": "boolean",
                "value": false
            }
        }
    }
    

    If it's false, then that means that the tab is hidden, and if it's true, then it means that the tab is shown.

    Although, note that this only detects if the tab is shown or hidden, so if there are two browser windows opened that are both shown (such as if they are next to each other), both will return true.