Selenium Python: Get the URL of the active tab. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Selenium Python: Get the URL of the active tab.

Hello everyone, I'm learning testing with Selenium. I want to get the url of the current active tab but no results. I wrote the following code: ``` links = my_span.find_elements_by_tag_name('a') # User clicks on the first link links[0].click() # link opens in a new tab print(self.browser.current_url) ``` it prints the url of the first tab, and never gets the url of the active tab. Any idea how can I get the URL of active tab in selenium/django? Thank you!

6th Sep 2019, 6:05 PM
N. A.
N. A. - avatar
1 Answer
0
Hello @wet water ))) Thank you for your reply :) It's more about selenium. Sometimes things get weird and confusing. I could solve it like this: ``` link[0].click() sleep(10) # wait till the tabs are loaded self.assertEqual(len(self.browser.window_handles), 2) self.browser.switch_to.window(self.browser.window_handles[1]) # switch to new tab # Make sure its url of google. self.assertEqual(self.browser.current_url, 'https://www.google.com/') ```
8th Sep 2019, 5:48 AM
N. A.
N. A. - avatar