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!
2 Respostas
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/')
```