Selenium Tutorial
Selenium is a toolset for automating web browser operations.
Selenium is mainly used for the automated testing of web applications.
Selenium supports multiple programming languages (such as Python, Java, C#, JavaScript, etc.) and can run on various 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 operations in the browser, such as clicking buttons, entering text, and navigating pages.
Who should read this tutorial?
-
Software Testing Engineer
-
Developer(Front-end, Back-end, Full-stack)
-
Data Analyst and Data Scientist
-
DevOps Engineer
-
Students and Programming Enthusiasts
-
Technical Manager and Team Leader
-
Entrepreneur and Product Manager
Selenium is a powerful automation testing tool, suitable for the automated testing of web applications and other browser automation tasks.
Through Selenium, you can write scripts to simulate user operations 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(e.g., Git; optional)
-
Testing fundamentals(e.g., unit testing, assertions; optional)
-
Operating system and command-line basics
-
Learning resources(official documentation, tutorials, books)
Learning path
-
Learn programming basics(e.g., Python or Java).
-
Learn web development basics (HTML, CSS, JavaScript).
-
Get familiar with browser developer tools.
-
Learn Selenium basics(environment setup, element location, element operations).
-
Hands-on projects(e.g., automated login, form submission, data scraping, etc.).
-
Learn advanced topics(e.g., wait mechanisms, browser operations, test framework integration).
Write your first Selenium script
Below is a simple Selenium script example written in Python; it opens the Chrome browser, navigates to Baidu's homepage, types "Runoob" into the search box, and clicks the search button.
Example
from
selenium
import
webdriver
from
selenium.
webdriver
.
common
.
keys
import
Keys
Create a Chrome WebDriver instance
driver
=
webdriver.
Chrome
(
)
Open the Baidu home page
driver.
get
(
"https://www.baidu.com"
)
Find the search box element
search_box
=
driver.
find_element_by_name
(
"wd"
)
Type "Runoob" into the search box
search_box.
send_keys
(
"Runoob"
)
Simulate pressing the Enter key
search_box.
send_keys
(
Keys.
RETURN
)
Close the browser
driver.
quit
(
)
Code Analysis
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 whose name attribute is "wd" (i.e., the search box).search_box.send_keys("Runoob"): Type "Runoob" into 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 site: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