Python + Selenium Web automated testing (1) Selenium installation

principle:

Selenium is an automation framework for web pages. Through it, we can write automated programs and operate the web interface in a browser like a human. For example, click the interface button, enter text in the text box and so on. You can also get information from the web interface. For example, get 12306 ticket information, job information on a job site, and so on.

The Selenium organization provides Selenium client libraries in multiple programming languages, including Java, Python, JavaScript, Ruby, etc., to facilitate the use of developers of different programming languages.

installation:

1. Selenium library

The installation of the Selenium environment is mainly to install two things: client library and browser driver.

Installing the Selenium library is very simple, just open the command line and enter the following command:

pip install selenium

It can also be installed directly in IDE such as Pycharm, as shown in the figure:

2. Install the browser driver (take Google Chrome as an example)

First check the Google browser version number, as shown in the figure, click "About Google" in the settings:

open the link below to download the Chrome browser driver
Chrome browser driver download address

Open the directory and download the compressed package of the corresponding system. If we are a Windows platform computer, download chromedriver_win32.zip
and unzip the program file chromedriver.exe inside to the Python root directory (so that there is no need to configure environment variables)

At this point, the environment setup is complete

Simple code demonstration:

The following code can automatically open the Chrome browser and automatically open the Baidu website.

from selenium import webdriver
import time

# 创建 WebDriver 对象,指明使用chrome浏览器驱动
wd = webdriver.Chrome()

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

# 休眠5秒后自动关闭
time.sleep(5)
wd.quit()

Guess you like

Origin blog.csdn.net/UCB001/article/details/106164080