To get the current URL of a web page when using Selenium in Python, you can use the Selenium webdriver current_url attribute.
from selenium import webdriver
driver = webdriver.Chrome()
driver.get("https://daztech.com/")
print(driver.current_url)
#Output:
https://daztech.com/
The Selenium Python module gives you the tools you need to be able to automate many tasks when working with web browsers.
When working with web browsers, sometimes you may want to get the current URL of the page you are working with after getting that web page with your web driver.
You can easily get the current URL of a web page when using Selenium in Python – just access the current_url attribute of your webdriver object.
Below is a simple example of how to get the current URL using Selenium in Python.
from selenium import webdriver
driver = webdriver.Chrome()
driver.get("https://daztech.com/")
print(driver.current_url)
#Output:
https://daztech.com/
Getting the Current URL After Redirects Using Selenium
One situation where you might want to get the current URL is if the web page you are trying to access redirects you to another page. In this case, to keep track of the URL, you will want to get the current URL.
In this case, it might be wise to sleep your Python code so that you can wait for the redirect to complete and then you can try and get the URL after all redirects are done.
Below is an example showing how you might use the Python sleep() function with Selenium.
from selenium import webdriver
import time
driver = webdriver.Chrome()
driver.get(some_url_with_redirects)
time.sleep(5)
current_url = driver.current_url
Hopefully this article has been useful for you to learn how to get the current URL of the web page you’ve opened using Selenium in Python.