• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / Get Current URL with Selenium in Python

Get Current URL with Selenium in Python

May 20, 2022 Leave a Comment

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("http://theprogrammingexpert.com/")

print(driver.current_url)

#Output:
http://theprogrammingexpert.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("http://theprogrammingexpert.com/")

print(driver.current_url)

#Output:
http://theprogrammingexpert.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.

Other Articles You'll Also Like:

  • 1.  Does Python Use Semicolons? Why Python Doesn’t Use Semicolons
  • 2.  Python Get Operating System Information with os and platform Modules
  • 3.  Replace Character in String in Python
  • 4.  Find Magnitude of Complex Number in Python
  • 5.  Deque Peek and Queue Peek Functions in Python
  • 6.  Rename Key in Dictionary in Python
  • 7.  How to Group By Columns and Find Mean in pandas DataFrame
  • 8.  pandas Absolute Value – Get Absolute Values in a Series or DataFrame
  • 9.  Find Median of List in Python
  • 10.  Python tostring method – Convert an Object to String with str() Function

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy