Python|crawlers and testing|installation and initial use of selenium framework (1)

Foreword:

As a glue language, Python can be said to be very good. It can do everything, and it can do very well in some fields, especially in the field of crawlers and testing. This language can be said to have no rivals.

The reason for saying this is because if you want to use a crawler to crawl something or do some testing work for some projects, and the testing work is still relatively cumbersome, then Python can help you achieve complete automation, thus freeing your hands And the brain, and you don’t think about any other languages, although these functions can be realized by other languages, for example, the test work can be realized by Java language, js language can also be realized, if you look at it from the perspective of development efficiency , the development efficiency will be a fraction or even a tenth of that of Python. If you look at it from the perspective of operating efficiency, then other languages ​​may be a little bit faster than Python, but who cares? ?

Simply from the perspective of testing this work, there are many tools that can be implemented, such as jmeter, ab, selenium, TestDirector, silktest, apifox, loadrunner and other tools. These tools can be divided into stress testing, performance testing, Functional testing and more.

Then, selenium is a better software in functional testing, but it needs to be developed in Python language, which is difficult to use (it is close to the bottom layer of the front end, and the operation is completely controlled at the code level)

Selenium can do stress testing, functional testing and performance testing, but it mainly focuses on performance testing. The outstanding advantage is that the accuracy of the software is very high because it completely simulates manual clicks.

So, this article will introduce how to install and deploy selenium and a demonstration of selenium simulated login, which is a basic performance test.

one,

The architecture of the selenium framework

The selenium framework can run under Python2 or Python3, and basically does not choose the Python version, which is a more friendly place.

Secondly, you need a browser, such as Google Chrome, Firefox browser, eg browser, IE browser, etc. These four commonly used browsers are generally supported, but the browser version is best not to be too high , which is close to the browser version the user is using.

Finally, selenium needs the driver of the browser. Generally, the driver download address of Google Chrome is: CNPM Binaries Mirror    . Special attention should be paid here. The version of the driver should be consistent with the version of the browser, and the difference should not be too large, otherwise selenium may It will start to report an error.

If selenium is successfully started, it will open a browser instance through the driver, and click or input according to the defined actions in your code, such as automatically logging in to a website (this website may be the entrance of your project) Of course, with the advancement of technology, a higher version of selenium will open a headless browser in the background, which means that you cannot see this browser, but it will do the actions you define.

Special Note:

The testing work is generally on the application side, which can also be understood as client-side work (that is, the b-side), and 90% of the projects are deployed on the Linux side (generally, the projects that need to be tested are b/s architecture), but Clients generally use Windows. Therefore, selenium is mainly installed on the Windows side. I was confused when I first started learning. It is usually not necessary to install selenium under Linux.

The version of Python used in this experiment is 3.8.1, and the version of Google Chrome is version 86

 

Driver download must pay attention to win32 

 

two,

The Windows deployment environment is very simple, there is nothing to say, even the Python installation is embarrassing, it is very simple, and the next step of the foolishness is over.

Selenium is also very simple, just one command: pip install selenium, just need to pay attention here, since Python installation packages are basically foreign, so it needs to be localized:

To be more precise, enter the command in the resource manager bar: %appdata%, this command is to locate the folder, create a new folder named pip in this directory, and create a new text file named pip.ini in the newly created folder. Write the following:

[global]
index-url=http://mirrors.aliyun.com/pypi/simple/
timeout = 6000
[install] 
trusted-host=mirrors.aliyun.com

Open cmd and execute the above installation command pip install selenium  :

Pay attention, the selenium installed here is version 4.11.2. There are many differences in the usage of selenium above version 4.0 and below version 4.0. This example takes version 4.0 and above as an example, not to mention version below 4.0. 

three,

Run the first selenium and open Baidu's homepage

You need to place the Google Chrome driver in the same layer directory as the Python executable file. The specific steps are as follows:

1,

Query the installation location of Python

2,

The driver extracted from the chromedriver_win32.zip file can be placed in the directory queried above. 

3,

Write a Python file with the following contents:

from selenium import webdriver

# 初始化浏览器为chrome浏览器
browser = webdriver.Chrome()

# 访问百度首页
browser.get(r'https://www.baidu.com/')

# 关闭浏览器
browser.close()

4,

Run this Python file in the cmd window

If the Google Chrome version does not match the driver version, an error will be reported as follows:

(I used the 114 driver, but the version of Google Chrome is 86, so an error is reported)

 Normal run without error:

 Four,

Keep selenium's window open

Add an infinite loop to the code:

from selenium import webdriver

# 初始化浏览器为chrome浏览器
browser = webdriver.Chrome()
while 2>0:

# 访问百度首页
  browser.get(r'https://www.baidu.com/')

# 关闭浏览器
browser.close()

The effect is as follows:

So this method is relatively rough, is there a more elegant method?

from selenium import webdriver

# 获取配置对象 => 什么样的浏览器就选择什么浏览器配置
option = webdriver.ChromeOptions()
option.add_experimental_option("detach", True)

# 获取driver对象, 并将配置好的option传入进去
driver = webdriver.Chrome(options=option)
driver.get('https://www.baidu.com')

To be continued! !

Guess you like

Origin blog.csdn.net/alwaysbefine/article/details/132482698