Get Current URL using Selenium for Python

Get Current URL

To get the current URL in the browser window using Selenium for Python, read the current_url property of web driver object.

driver.current_url

Example

  1. Initialise Chrome Web Driver object as driver.
  2. Get the URL https://pythonexamples.org/ using web driver object.
  3. Find element by link text 'OpenCV' and click on the element.
  4. Make an implicit wait of 5 seconds.
  5. Get the current URL to my_current_url variable.
  6. Print the current URL to output.

Python Program

from selenium.webdriver.chrome.service import Service
from selenium import webdriver
from selenium.webdriver.common.by import By

service = Service(executable_path="/usr/local/bin/chromedriver")
with webdriver.Chrome(service=service) as driver:
    driver.get('https://pythonexamples.org/')
    driver.find_element(By.LINK_TEXT, 'OpenCV').click()
    driver.implicitly_wait(5)
    my_current_url = driver.current_url
    print(my_current_url)

Output

https://pythonexamples.org/python-opencv/
Get Current URL using Selenium for Python

Summary

In this Python Selenium Tutorial, we learned how to navigate to a specific URL using driver.get() method.

Quiz on Selenium

Q1. What is the method used to find web elements in Selenium Python?

Not answered

Q2. What is the method used to switch to a different frame in Selenium Python?

Not answered

Q3. Which of the following is not a commonly used web element locator in Selenium Python?

Not answered

Q4. What is the method used to select a value from a drop-down list in Selenium Python?

Not answered

Q5. What is the method used to clear the text from an input field in Selenium Python?

Not answered
Code copied to clipboard successfully 👍