Selenium automated testing to achieve Baidu search

Table of contents

1. Install selenium third-party library

2. Install the browser driver

3. Configure driver environment variables 

1. Project environment variables

2. System environment variable configuration

4. Project actual combat

5. Complete code

 


1. Install selenium third-party library

Use pip to install the third-party library selenium, directly use pip to install the third-party library, the speed will be slower, you can use the domestic source to download and install. This will be faster.

pip install selenium

Use the source to install the third-party library selenium, here I use Tsinghua source as an example:

 pip install selenium -i https://pypi.tuna.tsinghua.edu.cn/simple


2. Install the browser driver

Driver download link:

http://chromedriver.storage.googleapis.com/index.html

Different Google Chrome versions, you must choose the version corresponding to your browser 


Open Google Chrome, check your browser version, click the three dots in the upper right corner, open the drop-down menu, and click Settings. 

 After entering the setting meeting, click About Chrome, the browser must download the corresponding version, otherwise it cannot be used


3. Configure driver environment variables 

1. Project environment variables

Create a project, and put the downloaded driver into the project folder after the project is created.


2. System environment variable configuration

Click on This PC, click Properties

 Go to System Settings, select Advanced System Settings.

Enter the interface, click Environment Variables, and configure environment variables. There are two types of environment variables, one is user variables and the other is system variables. There is not much difference between user variables and system variables. , which can be used by different users.  After the environment variables are configured, some computers need to restart the IDE, or shut down and restart. Test with code. See if the environment variable is called.

from selenuim import webdriver 
import time  
driver = webdriver.Chrome()  
driver.get('http://www.baidu.com')  
#等待3秒后自动关闭
time.sleep(3)  
driver.quit()  

 Use the code to open Baidu normally and then close it, which proves that the configured environment variables can be used normally.

There is little difference between these two configurations. The key point is that after the system environment variables are configured, there is no need to frequently configure the driver when creating a new project. Relatively speaking, it is relatively simple and less troublesome to directly configure the project environment.


4. Project actual combat

1. Create a new project, create a python file, and import three packages.


2. After the package is imported, the automatic operation code can be compiled. Open Baidu web page operation. Get the Baidu web page link, open Google Chrome, and enter "www.baidu.com" in the URL bar to search. And copy down the complete web link.

Check the web page request method, there are two types of web page request methods: "get \ host". So how to check it. Press F12 on the keyboard, different computers open in different ways, and most computers use F12.

 

 After obtaining the request method, you can edit the code to realize the operation of opening the webpage.

host = webdriver.Chrome()
host.get("https://www.baidu.com")  # 打开百度

This is the code that automatically opens the webpage operation.


3. Enter the content to be searched in the input box

First we need to find the XPATH of the search box.

 

Copy the value in the ID. Here I use ID to find, because ID is a unique value in the web page, it is always easy to find

# 找输入框
wd = host.find_element(by=By.NAME, value='wd')
# 输入对应的数据
ActionChains(host).move_to_element(wd).send_keys("123456").perform()

optimized code

# 找输入框,并输入
host.find_element(By.NAME,'wd').send_keys("123456")

       At this time, you can enter the required content in the webpage search box, but this step alone is not enough. The normal way to access the webpage is to enter the search content, click Search, and finally the webpage will return the search content. That's right, there is still the last step here, click the search button to operate.


4. Similarly, we need to find the location of the search button and use our code to click it.

Similarly, we also use the ID of the web page for indexing.

# 找"百度一下"按钮
su = host.find_element(by=By.ID, value='su')
# 执行点击操作
ActionChains(host).click(su).perform()

 optimized code

# 找"百度一下"按钮.并点击
host.find_element(By.ID, 'su').click()

Now we have achieved it, use the code to open Baidu, and automate the search.


5. Complete code

# 打开某网页
from selenium import webdriver
# 在网页上执行某操作
from selenium.webdriver import ActionChains
# 获取网页数据
from selenium.webdriver.common.by import By

host = webdriver.Chrome()
host.get("https://www.baidu.com")  # 打开百度
# 找输入框
wd = host.find_element(by=By.NAME, value='wd')
# 输入对应的数据
ActionChains(host).move_to_element(wd).send_keys("123456").perform()
# 找"百度一下"按钮
su = host.find_element(by=By.ID, value='su')
# 执行点击操作
ActionChains(host).click(su).perform()
"""
move_to_element()鼠标在标签位置停留
send_keys()在框内输入搜索内容
perform()执行操作,这是关键点,如果没有该操作前面的代码就不会执行相应的操作。
click()点击操作,相当与鼠标的单击
find_element()括号内是标签的地址,相当与XPATH表达式
find_element有多种用法,有ID,XPTATH,CLASS_NAME等多种表达式,这些必须使用英文大写,不然会报错
"""

 optimized code

# 打开某网页
from selenium import webdriver
# 在网页上执行某操作
from selenium.webdriver import ActionChains
# 获取网页数据
from selenium.webdriver.common.by import By

host = webdriver.Chrome()
host.get("https://www.baidu.com")  # 打开百度
# 找输入框输入对应的数据
host.find_element(By.NAME, 'wd').send_keys("123456")
# 找"百度一下"按钮,执行点击操作
host.find_element(By.ID, 'su').click()

"""
send_keys为输入
click未模拟点击操作
"""

 The usefulness in Selenium is not only these, but also many methods, so I won’t explain them one by one here, thank you for your support.

Guess you like

Origin blog.csdn.net/weixin_62854251/article/details/127590724