4

I am creating a webdriver instance using selenium for some automation work. I am opening multiple tabs at a time and would like to know if there is a way to get the URLs of all the tabs open.

driver=webdriver.Chrome()
driver.current_url

The above code provides me with the URL of the first tab only. Another thing I tried was:

driver.window_handles[0].current_url

The above solution failed as window_handles() returns a unicode object which does not contain current_url I prefer not going through all the tabs actively in order to find the current_url of each tab as it would disrupt the automation task at hand.

m0bi5
  • 8,900
  • 7
  • 33
  • 44
  • Check this https://stackoverflow.com/questions/28715942/how-do-i-switch-to-the-active-tab-in-selenium – karansthr Sep 26 '17 at 02:33
  • https://stackoverflow.com/questions/40458138/switch-between-tabs-and-perform-action-on-individual-using-selnium# – karansthr Sep 26 '17 at 02:34

1 Answers1

8

You just need to loop through each window handle, switch to it and print the url

for handle in driver.window_handles:
    driver.switch_to.window(handle)
    print(driver.current_url)
Tarun Lalwani
  • 142,312
  • 9
  • 204
  • 265
  • 1
    I get the idea behind this, but in this case the script should scroll through all the tabs in order to find the URLs. This is something I want to avoid – m0bi5 Sep 26 '17 at 12:49
  • 4
    You can't avoid that. you need to switch to window to get details of it, and only one window can be used at a time – Tarun Lalwani Sep 26 '17 at 13:05
  • Makes sense that selenium is unaware of the state of content in each tab until it arrives there. I imagine this is where selenium differs to the browser's engine to get more work done without communicating... – Yaakov Bressler Dec 14 '20 at 19:08