0
0
Selenium Pythontesting~10 mins

Page title and URL retrieval in Selenium Python - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test opens a web page, retrieves its title and URL, and verifies they match expected values.

Test Code - unittest
Selenium Python
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
import unittest

class TestPageTitleAndURL(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Chrome(service=Service())

    def test_title_and_url(self):
        driver = self.driver
        driver.get('https://example.com')
        title = driver.title
        url = driver.current_url
        self.assertEqual(title, 'Example Domain')
        self.assertEqual(url, 'https://example.com/')

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

if __name__ == '__main__':
    unittest.main()
Execution Trace - 6 Steps
StepActionSystem StateAssertionResult
1Test starts and Chrome browser opensChrome browser window is open, ready for commands-PASS
2Browser navigates to 'https://example.com'Browser displays the Example Domain homepage-PASS
3Retrieve page title using driver.titleTitle retrieved: 'Example Domain'Check if title equals 'Example Domain'PASS
4Retrieve current URL using driver.current_urlURL retrieved: 'https://example.com/'Check if URL equals 'https://example.com/'PASS
5Assertions for title and URL passTest confirms title and URL match expected valuesBoth assertions are truePASS
6Browser closes and test endsBrowser window closed, resources released-PASS
Failure Scenario
Failing Condition: Page title or URL does not match expected values
Execution Trace Quiz - 3 Questions
Test your understanding
What Selenium method is used to get the current page URL?
Adriver.current_url
Bdriver.get_url()
Cdriver.url()
Ddriver.page_url
Key Result
Always verify both the page title and URL to ensure the browser navigated to the correct page before proceeding with further tests.