0
0
Selenium Pythontesting~15 mins

Why Selenium is the standard for web automation in Selenium Python - Automation Benefits in Action

Choose your learning style9 modes available
Verify Selenium WebDriver can open a web page and check the page title
Preconditions (4)
Step 1: Open Chrome browser using Selenium WebDriver
Step 2: Navigate to 'https://www.selenium.dev/'
Step 3: Wait until the page is fully loaded
Step 4: Get the page title
Step 5: Verify the page title contains the word 'Selenium'
Step 6: Close the browser
✅ Expected Result: The browser opens the Selenium official website, the page title contains 'Selenium', and the browser closes without errors
Automation Requirements - Selenium with Python
Assertions Needed:
Page title contains 'Selenium'
Best Practices:
Use explicit waits to wait for page load
Use By locators 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.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

# Setup ChromeDriver service
service = Service()

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

try:
    # Open the Selenium official website
    driver.get('https://www.selenium.dev/')

    # Wait until the page title contains 'Selenium'
    WebDriverWait(driver, 10).until(EC.title_contains('Selenium'))

    # Get the page title
    title = driver.title

    # Assert the title contains 'Selenium'
    assert 'Selenium' in title, f"Title does not contain 'Selenium': {title}"

finally:
    # Close the browser
    driver.quit()

This script uses Selenium WebDriver with Python to open the Chrome browser and navigate to the Selenium official website.

We use WebDriverWait with expected_conditions.title_contains to wait explicitly until the page title contains the word 'Selenium'. This is better than a fixed sleep because it waits only as long as needed.

We then get the page title and assert it contains 'Selenium'. If the assertion fails, it will raise an error with a clear message.

The try-finally block ensures the browser closes even if an error occurs, which is a good practice to avoid leaving browser windows open.

This simple test shows why Selenium is a standard: it can control browsers, wait for conditions, and verify web page content reliably.

Common Mistakes - 3 Pitfalls
{'mistake': 'Using time.sleep() instead of explicit waits', 'why_bad': 'Fixed sleeps can make tests slow and flaky because they wait too long or not enough.', 'correct_approach': "Use Selenium's explicit waits like WebDriverWait with expected conditions."}
Not closing the browser after test
Using incorrect or brittle locators like absolute XPaths
Bonus Challenge

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

Show Hint