0
0
Selenium Pythontesting~15 mins

Why browser control is the foundation in Selenium Python - Automation Benefits in Action

Choose your learning style9 modes available
Verify browser control by opening a webpage and checking the title
Preconditions (3)
Step 1: Open the Chrome browser using Selenium WebDriver
Step 2: Navigate to 'https://example.com'
Step 3: Wait until the page is fully loaded
Step 4: Get the page title
Step 5: Close the browser
✅ Expected Result: The page title should be 'Example Domain' and the browser should close without errors
Automation Requirements - Selenium with Python
Assertions Needed:
Assert that the page title equals 'Example Domain'
Assert that the browser window is closed after test
Best Practices:
Use explicit waits to wait for page load
Use try-finally to ensure browser closes
Use By locators properly if needed
Keep code readable and simple
Automated Solution
Selenium Python
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

# Setup ChromeDriver service
service = Service()

# Initialize WebDriver
driver = webdriver.Chrome(service=service)

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

    # Wait until the title is 'Example Domain'
    WebDriverWait(driver, 10).until(EC.title_is('Example Domain'))

    # Get the page title
    title = driver.title

    # Assertion
    assert title == 'Example Domain', f"Expected title 'Example Domain' but got '{title}'"

finally:
    # Close the browser
    driver.quit()

This script starts by importing necessary Selenium modules.

We create a Chrome WebDriver instance to control the browser.

We open the URL 'https://example.com' using driver.get().

We use an explicit wait to pause until the page title matches 'Example Domain'. This ensures the page loaded fully before we check.

We then get the page title and assert it matches the expected title.

Finally, we close the browser in a finally block to make sure it always closes even if the test fails.

This shows how controlling the browser is the foundation for automated testing because it lets us open pages, wait for content, and verify results.

Common Mistakes - 3 Pitfalls
Not using explicit waits and checking title immediately
Not closing the browser after test
Hardcoding sleep instead of waits
Bonus Challenge

Now add data-driven testing to open three different URLs and verify their titles

Show Hint