0
0
Selenium Pythontesting~5 mins

Page title and URL retrieval in Selenium Python

Choose your learning style9 modes available
Introduction

We get the page title and URL to check if the right webpage opened. It helps us confirm the website works as expected.

After clicking a link, check if the new page is correct by verifying its title and URL.
When testing login, confirm the user lands on the correct dashboard page by checking the title and URL.
During navigation tests, verify each page loads by checking its title and URL.
When automating form submissions, confirm the success page by checking its title and URL.
Syntax
Selenium Python
from selenium import webdriver

# Create a browser driver
driver = webdriver.Chrome()

# Open a webpage
driver.get('https://example.com')

# Get page title
title = driver.title

# Get current URL
url = driver.current_url

# Print title and URL
print(f"Title: {title}")
print(f"URL: {url}")

# Close the browser
driver.quit()

driver.title returns the page title as a string.

driver.current_url returns the current page URL as a string.

Examples
Open Google homepage and print its title and URL.
Selenium Python
driver.get('https://www.google.com')
print(driver.title)
print(driver.current_url)
Open a page that may not exist to see what title and URL are returned.
Selenium Python
driver.get('https://example.com/nonexistentpage')
print(driver.title)
print(driver.current_url)
Get title and URL before and after clicking a link to verify navigation.
Selenium Python
driver.get('https://example.com')
print(driver.title)
print(driver.current_url)

# After clicking a link
link = driver.find_element('link text', 'More information...')
link.click()
print(driver.title)
print(driver.current_url)
Sample Program

This script opens example.com, prints the title and URL, clicks the 'More information...' link, waits 2 seconds, then prints the new page's title and URL before closing the browser.

Selenium Python
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
import time

# Setup Chrome driver (make sure chromedriver is in PATH)
service = Service()
driver = webdriver.Chrome(service=service)

try:
    # Open example.com
    driver.get('https://example.com')
    print(f"Before clicking link:")
    print(f"Title: {driver.title}")
    print(f"URL: {driver.current_url}")

    # Find and click the 'More information...' link
    link = driver.find_element(By.LINK_TEXT, 'More information...')
    link.click()

    # Wait for page to load
    time.sleep(2)

    print(f"After clicking link:")
    print(f"Title: {driver.title}")
    print(f"URL: {driver.current_url}")

finally:
    driver.quit()
OutputSuccess
Important Notes

Getting the title and URL is very fast (O(1) time complexity).

These properties do not use extra memory beyond storing strings (O(1) space complexity).

Common mistake: Trying to get title or URL before the page fully loads may give old or empty values. Use waits if needed.

Use title and URL checks to confirm navigation worked instead of relying only on page elements.

Summary

Use driver.title to get the page title as a string.

Use driver.current_url to get the current page URL as a string.

Check these after navigation to confirm you are on the expected page.