0
0
Selenium Pythontesting~15 mins

Page title and URL retrieval in Selenium Python - Build an Automation Script

Choose your learning style9 modes available
Verify page title and URL after navigation
Preconditions (2)
Step 1: Open the browser using Selenium WebDriver
Step 2: Navigate to 'https://example.com'
Step 3: Retrieve the page title
Step 4: Retrieve the current URL
Step 5: Verify the page title is exactly 'Example Domain'
Step 6: Verify the current URL is exactly 'https://example.com/'
Step 7: Close the browser
✅ Expected Result: The page title matches 'Example Domain' and the URL matches 'https://example.com/' exactly.
Automation Requirements - Selenium with Python
Assertions Needed:
Assert that the page title equals 'Example Domain'
Assert that the current URL equals 'https://example.com/'
Best Practices:
Use explicit waits if needed (though not required here as page loads quickly)
Use Selenium's By selectors properly
Close the browser in a finally block or use context management
Keep code readable and simple
Automated Solution
Selenium Python
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
import unittest

class TestPageTitleAndURL(unittest.TestCase):
    def setUp(self):
        options = Options()
        options.add_argument('--headless')  # Run in headless mode for testing
        self.driver = webdriver.Chrome(service=Service(), options=options)

    def test_title_and_url(self):
        self.driver.get('https://example.com')
        title = self.driver.title
        current_url = self.driver.current_url
        self.assertEqual(title, 'Example Domain', f"Expected title 'Example Domain' but got '{title}'")
        self.assertEqual(current_url, 'https://example.com/', f"Expected URL 'https://example.com/' but got '{current_url}'")

    def tearDown(self):
        self.driver.quit()

if __name__ == '__main__':
    unittest.main()

The setUp method initializes the Chrome WebDriver in headless mode to run tests without opening a visible browser window.

The test_title_and_url method navigates to 'https://example.com', retrieves the page title and current URL, then asserts both match the expected values exactly.

The tearDown method ensures the browser closes after the test, freeing resources.

This structure uses Python's unittest framework for clear setup, test, and cleanup phases, making the test reliable and easy to maintain.

Common Mistakes - 3 Pitfalls
Not closing the browser after the test
{'mistake': 'Using hardcoded sleeps instead of waiting for page load', 'why_bad': 'Hardcoded waits slow down tests and can cause flaky results if the page loads faster or slower than expected.', 'correct_approach': "Use Selenium's built-in waits or rely on page load completion before assertions."}
Checking title or URL with partial matches or contains
Bonus Challenge

Now add data-driven testing with 3 different URLs and their expected titles

Show Hint