Web automation testing - how to interact with Selenium page data and elements? Teach you not to miss a step.

 

Table of contents

Foreword:

1. Introduction to Selenium

2. Install Selenium

1. Windows users install Selenium

2. Install the Chrome browser driver

3. Use Selenium for page data and element interaction

1. Start the browser

2. Visit the webpage

3. Find elements

4. Enter text

5. Click the button

6. Submit the form

4. Complete code example

V. Summary

 Web automated testing:


Foreword:

Web automation testing is a commonly used testing method. By simulating user operations and interaction with page elements in the browser, the functionality and stability of the page can be effectively checked. Selenium is a popular web automation testing tool. In this article, we will introduce how to use Selenium to interact with page data and elements.

1. Introduction to Selenium

Selenium is an automated web testing tool that supports multiple programming languages, including Java, C#, Python, etc. It can simulate user operations on the browser, such as entering text, clicking buttons, and submitting forms. The power of Selenium is that it can interact with a variety of browsers, including Chrome and Firefox, etc.

2. Install Selenium

Before using Selenium, you need to install the Selenium library and browser driver. This article takes the Python language as an example to introduce how to install the Selenium library and the driver of the Chrome browser.

1. Windows users install Selenium

Enter the following command on the command line to install the Selenium library:

pip install selenium

2. Install the Chrome browser driver

Download the Chrome browser driver and extract it to a certain folder, and add the path of the folder to the environment variable.

3. Use Selenium for page data and element interaction

Next, we will introduce how to use Selenium to simulate user operations on the page in Python code, such as entering text, clicking buttons, and submitting forms.

1. Start the browser

Before starting, you need to start the browser. Taking Chrome as an example, the code is as follows:

from selenium import webdriver

# 创建webdriver对象
browser = webdriver.Chrome()

2. Visit the webpage

Use the get() method to open a web page, as shown below:

browser.get('https://www.baidu.com')

3. Find elements

Elements on the page can be found using the find_element_by_XXXX() method, where XXXX can be attributes such as ID, class, name, tag, etc. For example, to find the search box on the Baidu page, the code is as follows:

search_box = browser.find_element_by_id('kw')

4. Enter text

Use the send_keys() method to enter text, the code is as follows:

search_box.send_keys('Selenium')

5. Click the button

If you want to click a link or button, you can use the click() method. For example, to find the "Baidu click" button on the Baidu page and click it, the code is as follows:

submit_button = browser.find_element_by_id('su')
submit_button.click()

6. Submit the form

If you want to submit a form, you can use the submit() method. For example, to find a login form, and submit it, the code is as follows:

login_form = browser.find_element_by_id('login-form')
login_form.submit()

4. Complete code example

The following is a complete example to simulate searching Selenium through Selenium and clicking on the first result of Baidu:

from selenium import webdriver

# 创建webdriver对象
browser = webdriver.Chrome()

# 访问百度
browser.get('https://www.baidu.com')

# 查找搜索框
search_box = browser.find_element_by_id('kw')

# 输入搜索文本
search_box.send_keys('Selenium')

# 查找“百度一下”按钮
submit_button = browser.find_element_by_id('su')

# 点击“百度一下”按钮
submit_button.click()

# 查找结果列表
result_list = browser.find_elements_by_xpath('//div[@class="result c-container "]')

# 点击第一条结果
first_result = result_list[0].find_element_by_tag_name('a')
first_result.click()

# 关闭浏览器
browser.quit()

V. Summary

Selenium is a powerful web automation testing tool that can simulate user operations on the browser and interaction with page elements. In this article, we introduce the installation and use of Selenium and how to interact with page data and elements, hoping to help you with web automation testing. The code example demonstrates how to use Selenium to search and click on the first result of Baidu, and readers can modify it according to their own needs.

It should be noted that when using Selenium for automated testing, it is necessary to ensure the stability and reliability of the test cases. At the same time, considering that the stability of the page may be affected by various factors, such as network delay, dynamic loading of JavaScript, etc., it is necessary to write robust code to deal with different situations.

Finally, for developers who need to automate web testing, Selenium provides a convenient way to test. By using Selenium, you can quickly test the functionality and stability of the page, improve testing efficiency, and ensure the quality of the website. 

[Automated Test Exchange]: 574737577 icon-default.png?t=N3I4http://qm.qq.com/cgi-bin/qm/qr?_wv=1027&k=L7ciHIyOnE5wR-WjW99VYUQELBbNvwEi&authKey=d28p8qACMAvFA0DEZRwQEVTHiANFRKlRSSnAMBo7rnwuJexGvBynhSCy   Z4CAZZz8&noverify=0&group_code=574737577Automated test:

 Web automated testing:

Automated Testing - Benefits:

Guess you like

Origin blog.csdn.net/Free355/article/details/130682899