Selenium (a): Principles and install, simple to use

1. selenium principle

1.1 selenium Introduction

Selenium is an automation framework for Web applications.

Through it, we can write the automatic program, like humans operate web interface in your browser. Click interface such as a button, enter text in the text box and other operations.

But also to obtain information from the web interface. 12306 Ticket information such as access to job sites jobs, financial website stock price information, etc., and then analyze and process applications.

Selenium automation principle is this:

1.2 selenium client library

We write automation program needs to use a client library.

Our procedures are automated request is sent to the browser via the library inside the programming interface.

For example, we want to simulate a user clicks a button interface, automated procedures which function it should call the appropriate client library, click on the element will be sent to the browser requesting the bottom of the drive. Then, the browser and then drive forward the request to the browser.

This automated program sends to the browser-driven request is an HTTP request.

The client library come from? Selenium is an organization provides.

Selenium organization offers a variety of Selenium client library programming languages ​​including java, python, js, ruby, etc., enabling developers to easily use different programming languages.

We only need to install client library, call the library, you can send a request to the browser automation strategy.

1.3 browser driver

Browser drive is a stand-alone program, provided by browser vendors, different browsers require different browsers drive. For example, Chrome and Firefox browsers have different drivers.

After the browser receives the drive interface operation request sent our automated program, it will forward the request to the browser, so the browser to execute the corresponding automation.

After the browser performing the operation, the result will be automated driver returned to the browser, the browser and then drive through the HTTP response message is returned to our client library automation program.

After the client library automation program receiving the response, the result of the conversion returned to our code data objects.

Our program can know how the results of the automated operation.

1.4 summary

  1. Automation program calls Selenium client library functions (such as clicking a button element).
  2. Selenium client library sends a command to the driver to the browser.
  3. After the browser receives a command to the driver, the driver browser to execute the command.
  4. Browser execute the command.
  5. Browser driver gets the results of command execution, return to our automation program.
  6. Automated processing program to return the results.

2. Install selenium environment

Selenium major installation environment is to install two things: client library and browser driven.

2.1 Installing client library

Different programming languages ​​to select different Selenium client library. 

We correspond Python language, Selenium client library installation is very simple, you can use pip command.

Open a command line program, run the following command

pip install selenium

2.2 driver is installed browser

The browser and the browser is driven corresponding. Different browsers need to choose a different browser driven.

The current mainstream browsers, Chrome browser support for Selenium automation is more mature.

We have to Chrome browser, for example.

Install the latest version of the Chrome browser, click here to download

Chrome browser to access the driver download page, click here to download

Note that the browser must be driven and browser versions match, under the red circle inside the browser and version number is the version number corresponding.

 

For example: The current version of Google Chrome 78, you need to download the drivers directory 78 beginning.

Open the directory, there are three zip package, corresponding to Linux, Mac, Windows platform.

If we are the Windows platform computer, downloaded chromedriver_win32.zip

This is a zip package, after downloaded, extract the files inside the program chromedriver.exe to a directory below, note that this is not the best path to the directory and Chinese name spaces.

For example, extract it to E: \ webdrivers directory.

That is, to ensure that our Chrome browser path to drive E: \ webdrivers \ chromedriver.exe

The web-based selenium automation environment to build these is relatively simple. 

3. Simple to use 

3.1 Open your browser and enter the designated site

from selenium import webdriver

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

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

注意,等号右边返回的是WebDriver类型的对象,我们可以通过这个对象来操控浏览器,比如打开网址、选择界面元素等。

而下面这行代码,就是使用WebDriver的get方法打开网址百度 

wd.get('https://www.baidu.com')

执行上面这行代码时,自动化程序就发起了打开百度网址的请求消息,通过浏览器驱动,给Chrome浏览器。

Chome浏览器接收到该请求后,就会打开百度网址,通过浏览器驱动,告诉自动化程序打开成功。

3.2 关闭浏览器

执行完自动化代码,如果想关闭浏览器窗口可以调用WebDriver对象的 quit 方法,像这样 wd.quit() 。

3.3 浏览器驱动目录加入环境变量Path 

前面,我们的代码创建 WebDriver对象时,需要指定浏览器驱动路径,比如 

wd = webdriver.Chrome(r'E:\webdrivers\chromedriver.exe')

If we join the directory browser driver environment variables Path, when write code, you can specify the browser without having to drive a path, like this.

wd = webdriver.Chrome()

Because, those directories are automatically Selenium Path environment variable to specify where to find the file named chromedriver.exe.

Must pay attention to that, adding environment variables Path, not browser full drive path, such as E: \ webdrivers \ chromedriver.exe

Instead browser drive directory, for example E: \ webdrivers \

Guess you like

Origin www.cnblogs.com/liuhui0308/p/11926608.html