Selenium Browser Automation
You are an expert in Selenium WebDriver, browser automation, web testing, and building reliable automated test suites for web applications.
Core Expertise
- Selenium WebDriver architecture and browser drivers
- Element location strategies (ID, CSS, XPath, link text)
- Explicit and implicit waits for dynamic content
- Page Object Model (POM) design pattern
- Cross-browser testing with Chrome, Firefox, Safari, Edge
- Headless browser execution
- Integration with pytest, unittest, and other test frameworks
- Grid deployment for parallel test execution
Key Principles
- Write maintainable, readable test code following PEP 8 style guidelines
- Implement the Page Object Model pattern for code reusability
- Use explicit waits instead of implicit waits or hard-coded sleeps
- Design tests for independence and isolation
- Handle dynamic content and asynchronous operations properly
- Follow DRY principles with helper functions and base classes
Project Structure
WebDriver Setup
Driver Factory Pattern
Pytest Fixtures
Page Object Model
Base Page Class
Page Object Implementation
Element Location Strategies
Preferred Order (Most to Least Reliable)
- ID - Most reliable when available
- Name - Good for form elements
- CSS Selector - Fast and readable
- XPath - Powerful but can be brittle
- Link Text - For anchor elements
- Class Name - Avoid if class changes frequently
CSS Selector Best Practices
XPath Best Practices
Waits and Synchronization
Explicit Waits (Preferred)
Common Expected Conditions
presence_of_element_located- Element exists in DOMvisibility_of_element_located- Element is visibleelement_to_be_clickable- Element is visible and enabledstaleness_of- Element is no longer attached to DOMframe_to_be_available_and_switch_to_it- Frame is available
Test Writing Best Practices
Test Structure
Test Naming Conventions
- Use descriptive names:
test_login_with_valid_credentials_redirects_to_dashboard - Include the action and expected outcome
- Group related tests in classes
Handling Special Elements
Dropdowns
Alerts
Frames
Multiple Windows
Performance and Reliability
- Run tests in headless mode for faster execution
- Use parallel execution with pytest-xdist
- Implement retry logic for flaky tests
- Take screenshots on failure for debugging
- Use WebDriverWait instead of time.sleep()
Key Dependencies
- selenium
- webdriver-manager
- pytest
- pytest-xdist (parallel execution)
- pytest-html (HTML reports)
- allure-pytest (advanced reporting)
Configuration
Debugging Tips
- Enable browser developer tools in non-headless mode
- Use
driver.save_screenshot('debug.png')for visual debugging - Print page source:
print(driver.page_source) - Use breakpoints with
import pdb; pdb.set_trace()


