Get the current URL in Selenium web driver Python

Hello programmers, in this tutorial we will see how to get the current URL visited in the Selenium web driver in Python.

Selenium is used for machine controlling web browsers. Selenium is often integrated with other programming languages such as Python, JavaScript, Ruby, etc. to automate this process.

Here we will see how to get the current URL in the Selenium web driver in Python.

Use the following command in your command prompt to download the Selenium web driver.

pip install selenium

URL – Uniform Resource Locator is the unique global address to locate the resources on the World Wide Web (WWW).

Before running the code, we need to install the chrome driver and set it to our system path.

Below is an illustration of how to get the current URL visited in the Selenium web driver in Python.

#Importing necessary Libraries
from selenium import webdriver

#get url function
def seleniumGetURL():
    #creating a webdriver object
    driver = webdriver.Chrome(executable_path='C:/path/to/dir/chromedriver.exe')
    driver.get("https://www.codespeedy.com/")  #opening the url
    return driver.current_url

#driver
if __name__ == "__main__":
    print(f'The current URL visited is: {seleniumGetURL()}')  #call the function

Output

The current URL visited is: https://www.codespeedy.com/

Explanation
We create a driver object for the chrome browser and then open the URL. We return the current URL visited by the selenium web driver from the ‘seleniumGetURL()’ and print it.

Leave a Reply

Your email address will not be published.