This article is an entry-level application of Python + Selenium for automated testing, including environment setup and simple examples. The basic idea is to use Firefox Selenium IDE plug-in to record scripts and generate Python code, using Python combined with Selenium and browser drivers to execute the script. Operating system is Windows, the process does not understand the place remember Baidu, such as do not know how to open the Windows CMD command line, do not know how to set environment variables, etc., because the premise of this article is that readers already have some basic knowledge.
Install Python, visit its official websiteDownload the latest version, it is recommended to choose Python 3 installation (do not choose the embeddable version) version, the installation of remember to check two items: add Python to the environment variable and install pip, must remember to select these two, in order to save some trouble for the later steps.
After the installation is complete, type python and pip at the CMD command line to verify that it is installed properly. If it does not prompt "not an internal or external command", then the installation is working properly, otherwise it may not have been added to the environment variables.
The easiest way to install the python selenium package is to type python -m pip install selenium at the CMD command line. Note that there is a pip, which is the reason why you checked the install pip box above. In the picture below, I'm already satisfied because my environment is already installed.
In addition to the above installation through pip can also be directly downloaded python selenium installer, you can selenium official website (/download/) to download, download and install through the command line with python install, this method is not introduced here.
Install Selenium IDE for Firefox, this step is for recording scripts with Firefox, but it is not necessary, because you can also write scripts directly by hand, it is just easier for newbies to record. Open Firefox, search for Selenium IDE in Add-ons, find the component as shown in the picture, and install it.
After restarting Firefox you can see the Selenium IDE option in the Tools menu, indicating successful installation.
The next step is to download some webdrivers. Note that Firefox is natively supported by Selenium, but it seems that not all versions of Firefox are supported, so we need to find some drivers, and it seems that Firefox is unstable in execution, while Google's Chrome is better, so we choose Chrome here. The drivers can be found on the Selenium website, including IE and other browsers.
The two marked in the picture above are used for Firefox and Chrome respectively. After downloading, save it and add it to the environment variable, for convenience, it is recommended that you can put it directly into the directory where you installed Python before. Note that you must ensure that the driver directory is added to the environment variable, otherwise it cannot be executed.
At this point, a simple Selenium environment is ready, the following is a simple demonstration of Baidu as an example.
1, open Firefox, start Selenium IDE
2, pop-up Selenium IDE window, Base URL input, the right side of the recording button to keep recording open (the default has been opened)
3, enter in the browser address bar, click Search, this time you can see the Selenium has been generated in the script information, you can click on the upper left corner of the green button to test it!
4, assuming that the end of our recording has been, and then the script will be exported to python code, select the file & gt; Export Test Case As & gt; Python, note that here is python 2, generally does not matter much, if your python environment is python 3 there may be times when you want to do some modifications to the code!
5, save as a python source file, i.e. with a .py extension
6, open the generated code, the default is to Firefox browser as driver, it is recommended to change to Chrome!
7, at this point you can execute the test through python, open the CMD command line, enter the directory where the python code was stored, and type python to execute
8, if the previous steps are correct, then it will automatically open the browser, open Baidu search. If there is an error, then we must gradually exclude, it is not a couple of words can be clear. In addition, it is recommended to use Sublime Text 3, after installation, directly open the py file, press Ctrl + B execution.
This article is just a simple description of the Selenium environment to build, Selenium has a lot of other things, such as the positioning of elements, distributed tasks, etc., are not in the scope of this article.
To this article on the combination of Python Selenium simple implementation of Web automation testing article is introduced to this, more related to Python Selenium Web automation testing content, please search for my previous articles or continue to browse the following related articles I hope that you will support me in the future!