Docker+selenium method to realize automatic health reporting

Docker+selenium method to realize automatic health reporting

This article takes the health reporting system of a certain university as an example to complete the automation operation of the web side. The technology stack used is as follows:
Docker \ Selenium \ Python \ yagmail \ ssh etc.

Basic idea:
1. Write code locally and test it
2. Create a new docker container and configure the environment
3. Upload the code to the server and copy it to the docker container
4. Unzip and debug the code, and delete the code after confirming that there is no problem with the code
6. Exit the container and make the container into a mirror
7. Instantiate the container with the image and mount the code

1. Write code locally and debug

First, let's take a look at the target web we need to operate on:
Login Page:

Login Page

Form page:

insert image description here

The operation is very simple. Just log in, check the options you want, and then click Submit.

Source code attached:

Main file:

Main File

Send emails via yagmail:

yagmail file

Note: The [Authorization Code] of the email address is used here, not the password. QQ mailbox can be operated in Settings => Account:

insert image description here

Selenium Automation:

Selenium Automation

Xpath positioning is used here, and the browser can directly capture it, which is very convenient:

insert image description here

Remember to add quit and stop at the end, otherwise the process will pile up and cannot be closed, causing a crash. After running locally without any problems, you can proceed to the next step.

2. Configure the Docker container environment

The environment configuration is performed by the following steps
1. Pull the python image
2. Create a new container using Python image
3. Install the third-party libraries required for operation
4. Install the chrome browser and chromedriver
5. Upload test files for debugging

Pull the python image <br /> To do this, you need to make sure your docker is installed correctly and pull the image using the following command:

docker image pull python

Use the following command to check whether it has been pulled locally:

docker image ls

Create a new container using the python image

Use the following command to create a new container and enter the container:

docker run -it python /bin/bash

Install the third-party libraries required for operation <br /> The library can be installed through a simple pip command, such as:

pip install selenium
pip install yagmail
pip install pytz 

insert image description here

After installation, you can debug and confirm. I won’t go into details here.

Install the Chrome browser and Chromedriver
The following command can be used for Ubuntu system, and Baidu for other systems:

# Download wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
# Install dpkg -i google-chrome-stable_current_amd64.deb

Before installing chromedriver, you need to check the version number of chrome and download the corresponding driver according to the version number:

# Check the chrome version google-chrome --version

Regarding chromedriver, I downloaded and unzipped it locally and transferred it to the server using ssh:
Download address: http://chromedriver.storage.googleapis.com/index.html
After downloading and decompressing, upload it using the ssh command:

scp your driver address/chromedriver root@server IP:server directory

Then copy it from the host to the container:

docker cp chromwdriver container name: container directory

The code is also uploaded in this way, and the subsequent related operations no longer need to repeatedly enter the python command for simple debugging:

insert image description here

insert image description here

Upload the test file to confirm:

Test Files

If the email can be output and sent correctly, you can proceed to the next step

insert image description here

Make the container into an image

After deleting the test file, enter exit to exit the container and use the following command to create the image:

sudo docker commit eafd9111ada6 docker/sele_heath

The value after commit is the container ID or container name. If you are not sure, you can enter docker ps -a to view it.
docker/sele_heath is the name of the new image

After completion, you can enter the following command to view the newly created image:

docker images docker/sele_heath

Instantiate a container using an image and mount the code

After the image is built, you can proceed to the last step. The command is as follows:

docker run -d -v /home/admin/heath2:/usr/src -w /usr/src docker/sele_heath python main.py

Command analysis:
-d: block the process
-v: address mapping
/home/admin/heath2: Directory of code files
/usr/src: Mapped to the directory corresponding to the container
-w /usr/src: The directory for executing files inside the container, which is usually the same as the mapped directory
docker/sele_heath: image name
python main.py: Use python to run the main.py file

Enter the following command to check whether the container is running properly:

docker ps 

insert image description here

At this point, all the operations have been completed. All I need to do is get up and pick up my phone to check my emails :

insert image description here

This is the end of this article about how to achieve automated health reporting with Docker+selenium. For more relevant Docker selenium automation content, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Practical way to build selenium grid distributed environment with docker
  • Building a selenium distributed environment based on docker
  • How to use selenium+testng to realize web automation in docker
  • Sample code for testing technology application based on Docker+Selenium Grid

<<:  Vue component organization structure and component registration details

>>:  Tips for making HTML emails that can be displayed normally in mainstream mailboxes

Recommend

Solve the problem of MySql8.0 checking transaction isolation level error

Table of contents MySql8.0 View transaction isola...

Design theory: Why are we looking in the wrong place?

I took the bus to work a few days ago. Based on m...

Docker primary network port mapping configuration

Port Mapping Before the Docker container is start...

JavaScript implementation of the back to top button example

This article shares the specific code for JavaScr...

CSS solution for centering elements with variable width and height

1. Horizontal center Public code: html: <div c...

Docker data volume container creation and usage analysis

A data volume container is a container specifical...

Vue integrates a rich text editor that supports image zooming and dragging

need: According to business requirements, it is n...

How to run a project with docker

1. Enter the directory where your project war is ...

How to prevent event bubbling in JavaScript

What we need to pay attention to is that the char...

Example of how to quickly build a Redis cluster with Docker

What is Redis Cluster Redis cluster is a distribu...

Solve the installation problem of mysql8.0.19 winx64 version

MySQL is an open source, small relational databas...

JS implements the snake game

Table of contents 1. Initialization structure 2. ...