Home > Blockchain >  Is there any way to get all tab titles of browser without switching?
Is there any way to get all tab titles of browser without switching?

Time:11-14

I can get the all the opened tab titles by switching to each tab. Like below,

Code:

ArrayList<String> tabs = new ArrayList<String>(driver.getWindowHandles());
    
    for(int i = 0; i < tabs.size(); i  ) {
        driver.switchTo().window(tabs.get(i));
        System.out.println(driver.getTitle());
    }

Output:

Google
Home

Is there any way to get all the titles without switching? Something like below,

for(String tab : tabs){
  System.out.println(tab)
}

but the above code is returning the tab id's.

CDwindow-54AD3D4D73FFC6A60F79C1E56B131C2B
CDwindow-046B8E378703FEAAD1002BF3F835BAB7 

CodePudding user response:

Window Handle

Window Handle is a unique identifier that holds the address of all the windows. It as a pointer to a window, which can return the string value. It is assumed that each browser will have a unique window handle. This window handle 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.


Page Title

The Page Title is the <title> tag, which is a short description of a webpage and appears at the top of a browser window and is part of the HTML DOM within the <header>.

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


Conclusion

So there is no way to get all the tab titles of the browser without switching to specific TABs.

CodePudding user response:

With Selenium, No. 

But

With Helium

There is a tool called helium, you don't need to switch to the browser tab to interact with web elements.

  • Window management : Helium notices when popups open or close and focuses / defocuses them like a user would. You can also easily switch to a window by (parts of) its title. No more having to iterate over Selenium window handles.
  • iFrames : Unlike Selenium, Helium lets you interact with elements inside nested iFrames, without having to first "switch to" the iFrame.

But these good things come with a limation which is binding language, Helium is basically available for Python as of now.

  • Related