Introduction to Selenium
Selenium is the industry-standard tool for automating web browsers, trusted by millions of testers worldwide.
Selenium WebDriver is the most widely used browser automation tool, supporting multiple programming languages and browsers.
What is Selenium?
Selenium is an open-source suite of tools for automating web browsers. It provides a portable framework for testing web applications across different browsers and platforms.
Selenium Components
- Selenium WebDriver: The core component for browser automation
- Selenium Grid: Enables parallel test execution across multiple machines
- Selenium IDE: Record and playback tool for creating tests
Supported Languages
Selenium supports multiple programming languages:
- Java
- Python
- C#
- JavaScript (Node.js)
- Ruby
Basic Example (Python)
Here's a simple Selenium WebDriver test in Python:
python
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
# Initialize the Chrome driver
driver = webdriver.Chrome()
try:
# Navigate to Google
driver.get("https://www.google.com")
# Find the search box
search_box = driver.find_element(By.NAME, "q")
# Type a search query
search_box.send_keys("Selenium WebDriver")
search_box.send_keys(Keys.RETURN)
# Wait and verify results
assert "Selenium WebDriver" in driver.title
finally:
# Close the browser
driver.quit()Basic Example (JavaScript)
javascript
const { Builder, By, Key, until } = require('selenium-webdriver');
(async function example() {
// Create a new Chrome driver instance
let driver = await new Builder().forBrowser('chrome').build();
try {
// Navigate to Google
await driver.get('https://www.google.com');
// Find the search box and enter text
await driver.findElement(By.name('q')).sendKeys('Selenium WebDriver', Key.RETURN);
// Wait for and verify the title
await driver.wait(until.titleContains('Selenium'), 5000);
} finally {
// Quit the driver
await driver.quit();
}
})();