How to use Python+selenium for web automation testing?

Using Python+selenium to perform web automation testing is mainly divided into the following steps:

1. Install Python and selenium

First you need to install Python and selenium libraries. It can be installed through the pip command:

# 安装 Python
sudo apt-get install python3
 
# 安装 pip
sudo apt-get install python3-pip
 
# 安装 selenium 库
pip3 install selenium

2. Download the browser driver

Selenium requires a browser driver to operate the browser. You can select the corresponding driver based on the type of browser you are using and add it to the system path or specify the path in the code.

For example, if you use the Chrome browser, you need to download the ChromeDriver driver:

wget https://chromedriver.storage.googleapis.com/89.0.4389.23/chromedriver_linux64.zip
unzip chromedriver_linux64.zip
sudo mv chromedriver /usr/local/bin/

3. Write test scripts

Write automated test scripts to simulate user operations through the API provided by Selenium and check the results. Here's a simple example:

from selenium import webdriver
 
# 创建WebDriver对象,启动浏览器
driver = webdriver.Chrome()
 
# 打开网页
driver.get('https://www.baidu.com')
 
# 查找搜索框并输入内容
input_box = driver.find_element_by_css_selector('#kw')
input_box.send_keys('selenium')
 
# 查找“百度一下”按钮并点击
submit_button = driver.find_element_by_css_selector('#su')
submit_button.click()
 
# 检查搜索结果中是否包含关键字
assert 'selenium' in driver.title
 
# 关闭浏览器
driver.quit()

The above code uses the Chrome browser, opens the Baidu homepage, enters "selenium" and submits the search, and checks whether the search results page title contains "selenium". The script can be modified as needed.

4. Run the test script

To run automated test scripts, you can use the command line or any IDE debugger to execute the script.

For example, to execute the script using the Python interpreter:

python3 test.py

After running the script, the Chrome browser is launched and user operations are simulated, and the results are finally checked. If everything is OK, you can see the output "OK" on the console.

This is just a simple example. In fact, web automation testing involves many aspects, such as element positioning, exception handling, report generation, concurrent execution and other issues, which need to be designed and expanded according to actual needs.

Finally, I would like to thank everyone who reads my article carefully. Reciprocity is always necessary. Although it is not a very valuable thing, if you can use it, you can take it directly:

insert image description here

Software testing interview applet

A software test question bank that has been used by millions of people! ! ! Who is who knows! ! ! The most comprehensive interview test mini program on the Internet, you can use your mobile phone to answer questions, take the subway, bus, and roll it up!

Covers the following interview question sections:

1. Basic theory of software testing, 2. web, app, interface function testing, 3. network, 4. database, 5. linux

6. Web, app, interface automation, 7. Performance testing, 8. Programming basics, 9. HR interview questions, 10. Open test questions, 11. Security testing, 12. Computer basics

These materials should be the most comprehensive and complete preparation warehouse for [software testing] friends. This warehouse has also accompanied tens of thousands of test engineers through the most difficult journey. I hope it can help you too!  

Guess you like

Origin blog.csdn.net/nhb687095/article/details/132581680