How do I get current URL in Selenium Webdriver 2 Python?

PythonSeleniumSelenium Webdriver

Python Problem Overview


I'm trying to get the current url after a series of navigations in Selenium. I know there's a command called getLocation for ruby, but I can't find the syntax for Python.

Python Solutions


Solution 1 - Python

Use current_url element for Python 2:

print browser.current_url

For Python 3 and later versions of selenium:

print(driver.current_url)

Solution 2 - Python

According to this documentation (a place full of goodies:)):

driver.current_url

or, see official documentation: https://www.selenium.dev/documentation/en/webdriver/browser_manipulation/#get-current-url

Solution 3 - Python

Selenium2Library has get_location():

import Selenium2Library
s = Selenium2Library.Selenium2Library()
url = s.get_location()

Solution 4 - Python

Another way to do it would be to inspect the url bar in chrome to find the id of the element, have your WebDriver click that element, and then send the keys you use to copy and paste using the keys common function from selenium, and then printing it out or storing it as a variable, etc.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser2276896View Question on Stackoverflow
Solution 1 - Python4d4cView Answer on Stackoverflow
Solution 2 - PythonpbaranskiView Answer on Stackoverflow
Solution 3 - PythonMilankaView Answer on Stackoverflow
Solution 4 - PythonLiamººTView Answer on Stackoverflow