Selenium Tutorial

Selenium is a toolset 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).

Selenium's core functionality is to simulate user operations in the browser, such as clicking buttons, entering text, navigating pages, etc.

Who is this tutorial for?

  • 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 operations in the browser, thereby improving testing efficiency.

Knowledge you need before reading this tutorial:

  • Programming fundamentals(such as Python, Java, JavaScript, etc.)

  • Web development fundamentals(HTML、CSS、JavaScript)

  • Browser developer tools(such as Chrome DevTools)

  • Version control tools(such as Git, optional)

  • Testing fundamentals(such as unit testing, assertions, optional)

  • Operating system and command line fundamentals

  • Learning resources(Official documentation, tutorials, books)


Learning Path

  1. Learn programming fundamentals(such as Python or Java).

  2. Learn web development fundamentals(HTML、CSS、JavaScript)。

  3. Familiarize yourself with browser developer tools。

  4. Learn Selenium basics(Environment setup, element locating, element operations).

  5. Practice projects(such as automated login, form submission, data scraping, etc.).

  6. 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 import webdriver
from selenium.webdriver.common.keys import Keys

# Create Chrome WebDriver instance
driver = webdriver.Chrome()

# Open 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 Baidu homepage.
  • driver.find_element_by_name("wd"): Find the element whose name attribute is "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