Selenium WebDriver
Selenium Theory & Interview Questions
Comprehensive review of Selenium concepts, theory, and commonly asked interview questions with detailed explanations.
What is Selenium?
Selenium Definition
Selenium is a suite of tools used to automate web-based applications. It's primarily used for automating functional and regression test cases for web applications.
What Selenium Can Do
- • Automate web-based applications
- • Functional testing automation
- • Regression testing automation
- • Cross-browser testing
- • Multi-platform support
Limitations
- • Cannot automate desktop applications
- • Requires AutoIT for desktop automation
- • Limited to web-based testing
- • No built-in reporting
Selenium Suite Components
Selenium IDE
- • Record and playback test cases
- • Uses Selenese commands
- • Saves tests in .html format
- • Firefox browser support only
- • Limited programming capabilities
Selenium WebDriver
- • Interface for writing test cases
- • 8 types of locators available
- • Multi-browser support
- • Multi-language support
- • Cross-platform compatibility
Selenium RC
- • Remote Control (outdated)
- • Similar to WebDriver
- • Legacy tool
- • Not recommended for new projects
Selenium Grid
- • Parallel test execution
- • Multiple platform support
- • Cannot write test cases
- • Only executes existing tests
- • Distributed testing capability
Selenium Locators
8 Types of Locators
Locators are used to locate elements on web pages. Each has different performance characteristics and use cases.
1. ID (Fastest)
Most reliable and fastest locator
2. Name
Second fastest, uses name attribute
3. ClassName
Uses CSS class attribute
4. TagName
Uses HTML tag names
5. LinkText
Exact text match for links
6. PartialLinkText
Partial text match for links
7. CSS Selector
CSS-based element selection
8. XPath
XML path-based selection
CSS Selector vs XPath
Feature | CSS Selector | XPath |
---|---|---|
Direction | Forward direction only | Forward and backward direction |
Performance | Faster execution | Slower than CSS |
Shadow DOM | Can locate shadow elements | Cannot locate shadow elements |
Complexity | Limited functionality | Rich functionality with axes |
Text Handling | Limited text-based selection | Excellent text() method support |
Absolute vs Relative XPath
Absolute XPath
• Starts with single forward slash (/)
• Forward direction only
• Faster execution
• Complete path from root
• More prone to failures
/html/body/div/form/input
Relative XPath
• Starts with double forward slash (//)
• Forward and backward direction
• Slower than absolute
• Can start from any point
• Less prone to failures
//input[@id='username']
Key Method Differences
get() vs navigate().to()
get() Method
- • Waits for all components to load
- • Present in WebDriver interface
- • Return type: void
- • Complete page loading
navigate().to() Method
- • Doesn't wait for complete loading
- • Present in Navigation interface
- • Return type: void
- • Supports forward/backward navigation
findElement() vs findElements()
findElement()
- • Locates single element
- • Return type: WebElement
- • Throws NoSuchElementException
- • Returns first matching element
findElements()
- • Locates multiple elements
- • Return type: List<WebElement>
- • Returns empty list if not found
- • No exception thrown
Actions vs Action
Actions (Class)
- • Contains action methods
- • click(), doubleClick(), contextClick()
- • moveToElement(), dragAndDrop()
- • build() method available
Action (Interface)
- • Contains perform() method only
- • Executes combined actions
- • Return type: void
- • Final execution step
Element Handling Strategies
Dropdown Handling
With <select> tag:
- • Use Select class
- • selectByVisibleText()
- • selectByIndex()
- • selectByValue()
Without <select> tag:
- • Click dropdown element
- • Use findElements() for options
- • Iterate with enhanced for loop
- • Compare and click desired option
Alert Handling
Switch to alert:
driver.switchTo().alert()
- •
accept()
- Click OK - •
dismiss()
- Click Cancel - •
getText()
- Get alert text - •
sendKeys()
- Enter text
Frame Handling
Switch to frame using:
- • Frame name
- • Frame ID
- • Frame index position
- • Frame WebElement
Navigation:
- •
defaultContent()
- To main window - •
parentFrame()
- To parent frame
Calendar Handling
Steps:
- 1. Click calendar object
- 2. Use while loop for navigation
- 3. Compare month/year values
- 4. Click next/previous buttons
- 5. Find and click desired date
Knowledge Check
Question 1 of 5
Which is the fastest locator in Selenium?
Interview Preparation Summary
🎯 Core Concepts
- • Selenium is a suite of 4 tools
- • WebDriver is the main automation interface
- • 8 locators with ID being fastest
- • CSS Selector vs XPath trade-offs
⚡ Performance Tips
- • Use ID locator whenever possible
- • Prefer CSS Selector over XPath
- • Headless browser for faster execution
- • Relative XPath for maintainability
🔧 Best Practices
- • Handle exceptions properly
- • Use appropriate wait strategies
- • Switch contexts for frames/alerts
- • Implement proper element handling
🚨 Common Pitfalls
- • Not switching to frames before interaction
- • Using absolute XPath in frameworks
- • Ignoring wait strategies
- • Not handling dynamic elements properly