[Use of python's selenium library] Nanny-level basic tutorial, you will know it at a glance

1. Install selenium

You can install selenium with pip:

pip install selenium

2. Install the browser driver

To test a browser with selenium, you need to download and install the appropriate browser driver. For example, if you want to test the Google Chrome browser, you need to download the Chrome driver. You can download drivers for various browsers at the following links:

https://selenium.dev/documentation/en/webdriver/driver_requirements/

3. Create a test script

  1. Create a Python script and import the selenium library:
from selenium import webdriver
  1. Create a browser instance using the webdriver object. For example, if you're testing Google Chrome
driver = webdriver.Chrome()
  1. Execute the test script. Use the methods provided by selenium in the script to operate the browser, such as clicking buttons, entering text, and so on. For example, the following code will enter text into a search box and click the search button:
driver.get("https://www.example.com")  
search_box = driver.find_element_by_name("q")  
search_box.send_keys("hello world")  
search_button = driver.find_element_by_name("submit")  
search_button.click()

This code is an example of automated web page testing using the Selenium library. Here is a line-by-line explanation of the code:

  • driver.get("https://www.example.com"): This line of code uses the driver object provided by Selenium to access the specified URL, ie https://www.example.com.
  • search_box= driver.find_element_by_name("q"): This line of code uses the find_element_by_name() method of the driver object to locate the search box by the name of the element and returns a WebElement object that represents the search box. In this example, the element name of the search box is "q".
  • search_box.send_keys("hello world"): This line of code uses the send_keys() method of the search_box object to enter the text string "hello world" into the search box.
  • search_button = driver.find_element_by_name("submit"): This line of code uses the find_element_by_name() method of the driver object to locate the submit button by the name of the element and returns a WebElement object that represents the submit button. In this example, the element name for the submit button is "submit".
  • search_button.click(): This line of code uses the click() method of the search_button object to simulate the action of the user clicking the submit button.

  • The purpose of this code is to enter the text string "hello world" into the search box after visiting https://www.example.com, and submit a search request. This example takes advantage of the powerful features of the Selenium library to automate a series of browser operations for testing or automating web page interaction tasks.
  1. End the test. When the test is complete, you can close the browser with the following code:
driver.quit()

Guess you like

Origin blog.csdn.net/weixin_44045828/article/details/131686879