Selenium Tutorial
Selenium is a toolkit for automating web browser operations.
Selenium is mainly used for automated testing of web applications.
Selenium supports multiple programming languages (such as Python, Java, C#, JavaScript, etc.), and can run on multiple browsers (such as Chrome, Firefox, Edge, Safari, etc.) and operating systems (such as Windows, macOS, Linux).
The core function of Selenium is to simulate user actions in the browser, such as clicking buttons, entering text, navigating pages, etc.
Who should read this tutorial?
Software test engineers
Developers(Frontend, backend, full-stack)
Data analysts and data scientists
DevOps engineers
Students and programming enthusiasts
Technical managers and team leaders
Entrepreneurs and product managers
Selenium is a powerful automated testing tool, suitable for automated testing of web applications and other browser automation tasks.
With Selenium, you can write scripts to simulate user actions in the browser, thereby improving testing efficiency.
Knowledge you need before reading this tutorial:
Programming basics(such as Python, Java, JavaScript, etc.)
Web development basics(HTML、CSS、JavaScript)
Browser developer tools(such as Chrome DevTools)
Version control tools(such as Git, optional)
Testing basics(such as unit testing, assertions, optional)
Operating system and command line basics
Learning resources(official documentation, tutorials, books)
Learning Path
Learn programming basics(such as Python or Java).
Learn web development basics(HTML、CSS、JavaScript)。
Become familiar with browser developer tools。
Learn Selenium basics(environment setup, element location, element operations).
Practice projects(such as automated login, form submission, data scraping, etc.).
Learn advanced topics(such as wait mechanisms, browser operations, test framework integration).
Writing Your First Selenium Script
Below is a simple Selenium script example written in Python. This script opens the Chrome browser, visits the Baidu homepage, enters "Example" in the search box, and clicks the search button.
Example
from selenium.webdriver.common.keys import Keys
# Create a Chrome WebDriver instance
driver = webdriver.Chrome()
# Open the Baidu homepage
driver.get("https://www.baidu.com")
# Find the search box element
search_box = driver.find_element_by_name("wd")
# Enter "Example" in the search box
search_box.send_keys("Example")
# Simulate pressing the Enter key
search_box.send_keys(Keys.RETURN)
# Close the browser
driver.quit()
Code Explanation
webdriver.Chrome(): Create a Chrome WebDriver instance.driver.get("https://www.baidu.com"): Open the Baidu homepage.driver.find_element_by_name("wd"): Find the element with the name attribute "wd" on the page (i.e., the search box).search_box.send_keys("Example"): Enter "Example" in the search box.search_box.send_keys(Keys.RETURN): Simulate pressing the Enter key to trigger the search.driver.quit(): Close the browser.
Related Links
Official website:https://www.selenium.dev/
Selenium download:https://www.selenium.dev/downloads/
Selenium Chinese documentation:https://www.selenium.dev/zh-cn/documentation/
GitHub open source address:https://github.com/SeleniumHQ
Other Extensions