Python Selenium library installation and usage guide

Introduction

Selenium  is a powerful tool for automating browser operations. It can simulate user behavior in the browser, such as clicking, filling out forms, navigating, etc. In this guide, we will detail how to install and use  the Selenium library for Python  .

Install the Selenium library

The Selenium library can be installed via pip using the following command:

 pip install selenium

Install WebDriver

After installing selenium, we cannot directly use scripts to operate the browser for the time being. We also need to download the driver corresponding to the browser. Different browsers such as Chrome, edge, and Firefox need to download different drivers. At the same time, the driver also needs to correspond to the browser. For example, version 114 of the Chrome browser, the driver can only be Chromedriver of version 114.

After downloading WebDriver, add the path where its executable is located to your system's environment variables so Selenium can find it.

Example of use

1. First example: open a web page

 from selenium import webdriver
  # 创建一个 Chrome 浏览器实例
  driver = webdriver.Chrome()
  # 打开网页
  driver.get("https://www.baidu.com")
  # 关闭浏览器
  driver.quit()

2. Positioning elements

Selenium can be used to locate elements in a page in various ways, such as ID, class name, XPath, etc.

from selenium.webdriver.common.by import By
  element = driver.find_element(By.ID, "element_id")
  element = driver.find_element(By.CLASS_NAME, "element_class")
  element = driver.find_element(By.CSS_SELECTOR, "css_selector_expression")
  element = driver.find_element(By.XPATH, "//xpath_expression")

3. Fill out the form

input_element = driver.find_element(By.ID,"username")
  input_element.send_keys("your_username")
  password_element = driver.find_element(By.ID,"password")
  password_element.send_keys("your_password")
  submit_button = driver.find_element(By.ID,"submit_button")
  submit_button.click()

4. Click to operate

button = driver.find_element(By.ID,"button_id")
  button.click()

5. Wait for the element to load

Sometimes the page elements will not be loaded immediately and need to wait for a while. Waiting can be implemented using WebDriverWait and expected_conditions.

 from selenium.webdriver.common.by import By
  from selenium.webdriver.support.ui import WebDriverWait
  from selenium.webdriver.support import expected_conditions as EC
  wait = WebDriverWait(driver, 10)
  element = wait.until(EC.presence_of_element_located((By.ID, "element_id")))

Summarize

With this detailed guide, you should be able to start automating browser operations in Python using Selenium. According to needs, you can further learn how to handle more complex operations, handle pop-up windows, screenshots, etc. Selenium is a powerful tool that can play a huge role in scenarios such as automated testing , crawlers, and data collection.

Finally, I would like to thank everyone who has read my article carefully. Reciprocity is always necessary. Although it is not a very valuable thing, you can take it away if you need it:

These materials should be the most comprehensive and complete preparation warehouse for [software testing] friends. This warehouse has also accompanied tens of thousands of test engineers through the most difficult journey, and I hope it can help you! Partners can click the small card below to receive 

Guess you like

Origin blog.csdn.net/okcross0/article/details/132363159