'Get title of window without switching in selenium webdriver

Is there any way to get window title without making any switch in selenium?

presently I'm using below code:

public boolean switchToWindowByTitle(String title){
    String currentWindow = driver.getWindowHandle(); 
    Set<String> availableWindows = driver.getWindowHandles(); 
    if (!availableWindows.isEmpty()) { 
         for (String windowId : availableWindows) {
              String switchedWindowTitle=driver.switchTo().window(windowId).getTitle();
              if ((switchedWindowTitle.equals(title))||(switchedWindowTitle.contains(title))){ 
                  return true; 
              } else { 
                driver.switchTo().window(currentWindow); 
              } 
          } 
     } 
     return false;
}


Solution 1:[1]

Page Title is the <title> tag and appears at the top of a browser window which is part of the HTML DOM within the <header> section of the <html>.

So Selenium driven WebDriver needs to have the focus on the specific Browsing Context to extract the Page Title.

Where as Window Handle is a unique identifier that holds the address of all the windows and can return the string value. All the browser will have a unique window handle. This getWindowHandles() function helps to retrieve the handles of all windows.

So Selenium driven WebDriver can collect the Window Handles from the Browsing Context even without having individual focus on them.


Conclusion

So it is not possible to get the title of window/tab without switching to the specific Window / TAB.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 undetected Selenium