Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of Web Automation Test based on Python and Selenium

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article will explain in detail the example analysis of Python combined with Selenium to achieve Web automation testing, Xiaobian feels quite practical, so share it with you as a reference, I hope you can gain something after reading this article.

Install Python, visit its official website www.python.org to download the latest version, it is recommended to choose Python 3 installation (do not choose embeddable version) version, remember to check two items when installing: add Python to environment variables and install pip, be sure to select these two items, in order to save some trouble for the later steps.

After installation, enter python and pip on the CMD command line to verify whether the installation is correct. If there is no prompt "Not an internal or external command," it means that the installation is normal, otherwise it may not be added to the environment variable.

The easiest way to install the python selenium package is to enter python -m pip install selenium on the CMD command line. Note that there is a pip here, which is why we checked the installation pip above. In the image below, my environment is already satisfied because it is installed.

In addition to the above installation via ip, you can also download the selenium installation package of python directly. You can download it from the selenium official website (http://www.seleniumhq.org/download/). After downloading, you can install it by using python setup.py install on the command line. This method is not introduced here.

Install Selenium IDE for Firefox. This step is to record scripts in Firefox, but it is not necessary, because you can also write scripts directly by hand, but it will be convenient for beginners to record. Open Firefox, search Selenium IDE in add-ons, find the component shown in the figure, and install it.

After restarting Firefox, you can see the Selenium IDE option in the Tools menu, indicating that the installation is successful.

Next, download some browser drivers (webdrivers), note that Firefox is supported natively by Selenium, but it seems that not all Firefox versions support it, so we have to find some drivers, and it seems that Firefox is not stable when it executes, while Google's Chrome browser is better, so we choose Chrome here. The drivers can be found on Selenium's website, including IE and other browsers.

The two marked in the above figure are for Firefox and Chrome respectively. After downloading, save them and add them to the environment variables. For convenience, it is recommended to put them directly under the directory where Python was installed before. Note that you must ensure that the driver directory is added to the environment variable, otherwise it cannot be executed.

At this point, the simple Selenium environment is ready, the following Baidu as an example of a simple demonstration.

1, Open Firefox, launch Selenium IDE

2. Selenium IDE window pops up, enter http://www.baidu.com for Base URL, and keep recording on the right button (already on by default).

3. Enter www.baidu.com in the browser address bar and click Search. At this time, you can see that the script information has been generated in Selenium. You can click the green button in the upper left corner to test it.

4, assuming that our recording has ended at this time, and then export the script to python code, select File>Export Test Case As>Python, note that this is python 2, in general, it does not matter, if your python environment is python 3, sometimes you may have to make some changes to the code.

5, save as python source file, that is, with.py as suffix

6, Open the generated code, Default is Firefox browser as the driver, Suggest to change to Chrome

7. At this time, you can execute the test through python. Open the CMD command line, enter the directory where the python code is stored, and enter python test.py to execute it.

8, if the previous steps are correct, then the browser will automatically open, open Baidu search. If there are mistakes, they should be eliminated gradually, which is not something that can be explained clearly in a few words. In addition, it is recommended to use Sublime Text 3, open py file directly after installation, press Ctrl+B to execute.

About "Python combined with Selenium to achieve Web automation test sample analysis" This article is shared here, I hope the above content can be of some help to everyone, so that you can learn more knowledge, if you think the article is good, please share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report