Building a web automation environment based on Selenium


1. What is Selenium

Selenium is a program automation operation solution for a set of Web sites. This article mainly explains the deployment and construction of Selenium's automated operating environment.

2. Selenium's automation principle

insert image description here

3. Steps to build a selenium-based web automation environment

1. Install the Selenium client library

Based on the Python language, the Selenium client library can be installed with the pip command.
Open the command line program and run the following command:

pip install selenium

If you encounter an installation error, you can specify to use the domestic Douban source for installation:

pip install selenium -i https://pypi.douban.com/simple/

2. Install the browser and browser driver

The steps to install the browser will not be described in detail. It is recommended to install the Chrome browser. After installing the browser, you need to install the browser driver. Note 1: The browser driver must
match the browser version
.
Attachment: Chrome browser driver download address
If we are a computer with Windows platform, download chromedriver_win32.zip. After downloading, unzip the program file chromedriver.exe inside to a certain directory. The path of the directory should preferably have no Chinese name and spaces. For example, unzip it to D:\toolsthe directory below, and ensure that the drive path of our Chrome browser isD:\tools\chromedriver.exe

3. Verify whether the environment is built successfully

To verify whether the environment is built successfully, you can write a simple program and run it for a try. Run the following code, automatically open the Chrome browser, and automatically open the Baidu website, which proves that the environment is successfully built.
Sample code:

from selenium import webdriver
from selenium.webdriver.chrome.service import Service

# 创建 WebDriver 对象,指明使用chrome浏览器驱动
wd = webdriver.Chrome(service=Service(r'D:\tools\chromedriver.exe'))

# 调用WebDriver 对象的get方法 可以让浏览器打开指定网址
wd.get('https://www.baidu.com')

4. Optimizable methods

Add the directory where the browser driver is located to the environment variable Path. Note that it is not the full path of the browser driver, for example D:\tools\chromedriver.exe, but the directory where the browser driver is located. For example D:\tools,
when writing code:

wd = webdriver.Chrome(service=Service(r'D:\tools\chromedriver.exe'))

can be replaced by:

wd = webdriver.Chrome()

In this way, there is no need to specify the browser driver path in the code, because Selenium will automatically search for a file named chromedriver.exe in those directories specified by the environment variable Path.


Summarize

Recently, I started to learn the automation tool Selenium, and recorded some learning points from time to time, and will continue to update other new content later, just as study notes and experience sharing.

Guess you like

Origin blog.csdn.net/weixin_44996886/article/details/131117143