Learn this example, Xiaobai can also understand the principle of selenium

I. Introduction

Selenium is an open source framework for web automation testing.

It supports multiple languages: python/java/c#...

There is also an article in the front that explained the environment construction of selenium+ browser.

Selenium supports multiple languages ​​because the communication between selenium and the browser driver is through the http protocol.

It only cares about whether the communicated data can be interpreted correctly, and does not care about which client the data comes from. No matter it comes from python\java, or jmeter, postman is no problem.

In this article, using requests as the client, skipping selenium, communicating directly with the Google browser driver (chromedriver) for http communication, and driving the chrome browser to execute commands.

requests library

First explain the requests library:

A python third-party library is currently the best http request library. It directly encapsulates get request and post request.

You only need to provide the request url, request method, and request content.

The following is a simple example of the use of the request library (for detailed use of the request, please refer to other blog posts by other bloggers):

Learn this example, Xiaobai can also understand the principle of selenium

 

If I want to use the requests library to send a request to chromedriver.

Then I have to understand the type of request, the data requested, and the content of the request.

Based on this, we need to know what requests will be made in the selenium library?

2. Problems to be solved

1. What kind of requests does selenium have?

2. How to obtain the request url and request type of each request?

3. How to obtain the requested data for each request?

Three, get the request url and type

selenium-json wire protocol-get request url and type

To solve the above three problems, we need to understand some of the principles of selenium.

The full name of the http communication protocol between selenium and the driver is called:

json wire protocol。

When we use the selenium library to drive the browser, part of our operation is roughly as follows:

1) Open the chrome browser;

2) Visit a certain website;

3) Find an element in the URL;

4) Operate the elements found in 3).

In the view of the selenium library, each of the above operations is an http request, also called a command (Command).

After chromedriver receives this request, it then drives to perform corresponding actions on the chrome browser.

Learn this example, Xiaobai can also understand the principle of selenium

 

Therefore, in the selenium library, the names of all commands (Command), the http request type corresponding to the command, and the request URL corresponding to the command are stored.

First, let's take a look at the name of the Command (select a few well-known operations):

Learn this example, Xiaobai can also understand the principle of selenium

 

The request type and request url corresponding to the website access command (GET) are:

Learn this example, Xiaobai can also understand the principle of selenium

 

As can be seen from the above figure, the GET command is a post request, and the request address is only part of it.

There are 3 problems in the url:

1) The requested URL is not complete.

In the url, the base address is missing. The base address is the ip+port number of the chromedriver. Because the command is sent to chromedriver to execute.

2) What is the $sessionId in the url?

In selenium, every time a session with chromedriver is opened, a session ID is generated. sessionId is the session ID. In many command requests, the current session is bound by sessionId in the request address.

In other words, we need to use requests to communicate with chromedriver, then we first have to generate a session ID, and get this ID value, then we can visit the webpage and send more browser operation commands.

3) Where does the sessionId come from? How to get it?

In selenium, the session is opened through the NEW_SESSION request. After receiving the request, chromedriver returns the sessionId of the session in the response data.

Learn this example, Xiaobai can also understand the principle of selenium

 

The requested parameters are as follows (what type of browser is started, what configuration parameters are there):

Learn this example, Xiaobai can also understand the principle of selenium

 

After chromedriver receives the request normally, the response data is as follows (mainly sessionId):

Learn this example, Xiaobai can also understand the principle of selenium

 

Learn this example, Xiaobai can also understand the principle of selenium

 

Learn this example, Xiaobai can also understand the principle of selenium

 

Four, set the request data

selenium-every command function-set request data

Above we have obtained the request address and request type of each command.

So where does the request data come from?

The function of each command is different, and the requested data is also different. In selenium, the request data is set in the function corresponding to the command.

For example, to access the URL operation command, in Selenium is the get function, then let's look at the source code of the get function:

Learn this example, Xiaobai can also understand the principle of selenium

 

In the execute function in the figure above, the second parameter params corresponds to the request data.

So the request body of the get command is:

{"url": The url value passed in by calling the get function}

For another example, the find element command is the find_element function in selenium.

Then let's see the source code of find_element:

Learn this example, Xiaobai can also understand the principle of selenium

 

So the request body of the find_elment command is:

{"using":location type,"value":location expression}

Use requests

Use requests-open browser session, visit Baidu homepage, search for lemon class

1. Start the chromedriver program on the local computer.

Double-click, the default service port is 9515

Learn this example, Xiaobai can also understand the principle of selenium

 

2. Initiate a session to chromedriver through the requests library.

And the code to open the Baidu homepage is as follows:

Learn this example, Xiaobai can also understand the principle of selenium

 

Learn this example, Xiaobai can also understand the principle of selenium

 

Learn this example, Xiaobai can also understand the principle of selenium

 

The result of running the above code is as follows:

Learn this example, Xiaobai can also understand the principle of selenium

Recommend a software testing learning exchange group: 785128166, there are shared videos, interview guidance, test materials, mind maps, and videos in the group. They are all dry goods, you can download and watch. Mainly share test foundation, interface test, performance test, automated test, TestOps architecture, Jmeter, LoadRunner, Fiddler, MySql, Linux, resume optimization, interview skills, and actual video data of large-scale test projects. Use every minute and every second of your time to learn to improve yourself, and don't use "no time" to conceal your mental laziness! Try hard while you are young, and give your future self an explanation!

Public number: Programmer Erhei, after paying attention, you can receive a large amount of learning materials for free.

Good things should be shared with friends
 

Guess you like

Origin blog.csdn.net/weixin_53519100/article/details/114851011