1

I often open hundreds of tabs when using web browsers, and this slows my computer. So I want to write a browser manager in Python and Selenium , which opens tabs and can save the urls of those tabs, then I can reopen them later.

But it seems like the only way to get the url of a tab in Python Selenium is calling get_current_url.

I'm wondering if there's a way to get the url of a tab without switching to it?

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Kirk
  • 446
  • 4
  • 18
  • How you opens the tabs? By clicking the links in parent page? – santhosh kumar May 19 '17 at 10:10
  • In no specific ways. They can be opened by user, by javascript in the parent page, or by right clicking a link and select "open in new tab"… I guess it's not possible to fetch the url of a tab without switching to it in selenium, maybe selenium is not appropriate to do this. – Kirk May 19 '17 at 14:55

4 Answers4

0

Just go to the text link which is switching to other tab and save its @href attribute link into a string or list

Sandeep
  • 455
  • 4
  • 26
  • I don't understand. Tabs can be opened without clicking a link, it can be opened by user or redirect to a new url by javascript without any user's action! – Kirk May 19 '17 at 09:29
  • 1
    There's no way to achieve other tabs URL without navigating to it. As Selenium only works for the present active tab only. To get URL of other tab u need to switch to it – Sandeep May 19 '17 at 10:12
  • Thanks. Maybe I'll see how webdrivers work, or research on writing browser extensions, or try to fetch the url by win32 api: http://stackoverflow.com/questions/11645123/how-do-i-get-the-url-of-the-active-google-chrome-tab-in-windows – Kirk May 19 '17 at 14:41
0

I am not sure about your actual scenario but we can get the list of all hyperlinks present in the current page. The idea is to collect all web elements with tag "a" and later get their "href" attribute value. Below is a sample code in Java. Kindly modify it accordingly.

//Collecting all hyperlink elements
List<WebElement> allLinks = driver.findElements(By.tagName("a"));

//For each Hyperlink element getting its target href value 
for(WebElement link: allLinks)
{
    System.out.println(link.getAttribute("href"));
}

Hope this helps you. Thanks.

santhosh kumar
  • 1,981
  • 1
  • 9
  • 28
0

My recommendation is to use an extension for that or write/extend your own.
There seem to be some of those types like
https://addons.mozilla.org/en-US/firefox/addon/export-tabs-urls-and-titles/ or
https://chrome.google.com/webstore/detail/save-all-tab-urls/bgjfbcjoaghcfdhnnnnaofkjbnelkkcm?hl=en-GB

To my kowledge, there is no way of getting/accessing an url of a webpage without first switching to it.

0

There is no other way to get the specific tab titles of the browser without switching to the specific TAB as Selenium needs focus on the DOM Tree to perform any operation.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352