Bird
Raised Fist0
Selenium Pythontesting~5 mins

Page title and URL retrieval in Selenium Python

Choose your learning style10 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.

Practice

(1/5)
1. Which Selenium WebDriver property is used to get the title of the current web page?
easy
A. driver.current_url
B. driver.page_source
C. driver.title
D. driver.get_title()

Solution

  1. Step 1: Understand the purpose of driver.title

    The driver.title property returns the title of the current page as a string.
  2. Step 2: Compare with other properties

    driver.page_source returns the HTML source, driver.current_url returns the URL, and driver.get_title() is not a valid method in Selenium Python.
  3. Final Answer:

    driver.title -> Option C
  4. Quick Check:

    Page title = driver.title [OK]
Hint: Use driver.title to get page title quickly [OK]
Common Mistakes:
  • Confusing driver.title with driver.current_url
  • Using driver.get_title() which does not exist
  • Trying to get title from page_source
2. Which of the following is the correct syntax to get the current page URL in Selenium Python?
easy
A. url = driver.current_url
B. url = driver.get_current_url()
C. url = driver.url()
D. url = driver.getUrl

Solution

  1. Step 1: Recall the property for current URL

    In Selenium Python, driver.current_url is a property that returns the current page URL as a string.
  2. Step 2: Check syntax correctness

    driver.get_current_url(), driver.url(), and driver.getUrl use invalid method names or syntax. Only url = driver.current_url is correct without parentheses.
  3. Final Answer:

    url = driver.current_url -> Option A
  4. Quick Check:

    Current URL = driver.current_url [OK]
Hint: Remember current_url is a property, not a method [OK]
Common Mistakes:
  • Adding parentheses to current_url like a method
  • Using incorrect method names like get_current_url()
  • Trying to access URL via driver.url()
3. What will be the output of the following code snippet if the browser is currently on the page with title "Welcome Page" and URL "https://example.com/home"?
print(driver.title)
print(driver.current_url)
medium
A. Welcome Page https://example.com/home
B. https://example.com/home Welcome Page
C. driver.title driver.current_url
D. Error: driver.title and driver.current_url are methods

Solution

  1. Step 1: Understand what driver.title returns

    driver.title returns the page title string, here "Welcome Page".
  2. Step 2: Understand what driver.current_url returns

    driver.current_url returns the current URL string, here "https://example.com/home".
  3. Final Answer:

    Welcome Page https://example.com/home -> Option A
  4. Quick Check:

    Title then URL printed correctly [OK]
Hint: driver.title prints title; driver.current_url prints URL [OK]
Common Mistakes:
  • Swapping title and URL order in output
  • Thinking driver.title/current_url are methods needing ()
  • Expecting variable names printed instead of values
4. You wrote the code below to print the page title and URL, but it raises an AttributeError. What is the mistake?
print(driver.get_title())
print(driver.get_current_url())
medium
A. You must import get_title and get_current_url from selenium
B. driver.get_title() and driver.get_current_url() are not valid methods in Selenium Python
C. driver.title and driver.current_url are not accessible properties
D. driver.title and driver.current_url need parentheses

Solution

  1. Step 1: Check method existence in Selenium Python

    There are no methods named get_title() or get_current_url() in Selenium Python WebDriver.
  2. Step 2: Use correct properties

    The correct way is to use driver.title and driver.current_url without parentheses as they are properties, not methods.
  3. Final Answer:

    driver.get_title() and driver.get_current_url() are not valid methods in Selenium Python -> Option B
  4. Quick Check:

    Use properties, not get_ methods [OK]
Hint: Use driver.title and driver.current_url properties, no () [OK]
Common Mistakes:
  • Calling driver.title() or driver.current_url() as methods
  • Trying to import non-existent get_title functions
  • Confusing Selenium Python with other language APIs
5. You want to verify that after navigating to "https://example.com/login", the page title is exactly "Login Page" and the URL contains "/login". Which code snippet correctly performs these checks using Selenium Python?
hard
A. assert driver.title == "Login Page" or driver.current_url == "/login"
B. assert driver.title() == "Login Page" and driver.current_url() == "/login"
C. assert driver.get_title() == "Login Page" and driver.get_current_url() == "/login"
D. assert driver.title == "Login Page" and "/login" in driver.current_url

Solution

  1. Step 1: Use correct properties and operators

    Use driver.title to get the page title and driver.current_url to get the URL. Use == to check exact title and 'in' to check substring in URL.
  2. Step 2: Understand assertion logic

    Use 'and' to ensure both conditions are true. assert driver.title == "Login Page" or driver.current_url == "/login" uses 'or' which passes if only one condition is true, not both.
  3. Final Answer:

    assert driver.title == "Login Page" and "/login" in driver.current_url -> Option D
  4. Quick Check:

    Use properties and 'and' for combined checks [OK]
Hint: Use assert with driver.title and 'in' for URL substring [OK]
Common Mistakes:
  • Calling title or current_url as methods
  • Using 'or' instead of 'and' in assertions
  • Checking URL equality instead of substring
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard