6

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.

2

1 Answer 1

11

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)
Sign up to request clarification or add additional context in comments.

3 Comments

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
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
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...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.