Web automated testing - how to interact with Selenium page data and elements?

Foreword:

Web automated testing is a common testing method that can effectively test the functionality and stability of the page by simulating user operations and interactions with page elements in the browser. 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 multiple browsers, including Chrome and Firefox.

2. Install Selenium

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

1. Windows users install Selenium

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

pip install selenium

2. Install Chrome browser driver

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

现在我也找了很多测试的朋友,做了一个分享技术的交流群,共享了很多我们收集的技术文档和视频教程。
如果你不想再体验自学时找不到资源,没人解答问题,坚持几天便放弃的感受
可以加入我们一起交流。而且还有很多在自动化,性能,安全,测试开发等等方面有一定建树的技术大牛
分享他们的经验,还会分享很多直播讲座和技术沙龙
可以免费学习!划重点!开源的!!!
qq群号:110685036

3. Use Selenium to interact with page data and elements

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, etc.

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 web page

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

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

3. Find elements

You can use the find_element_by_XXXX() method to find elements on the page, where XXXX can be attributes such as ID, class, name, tag, etc. For example, to find the search box on a 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, look for 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

Here is a complete example that simulates searching Selenium through Selenium and clicking on the first result on 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()

5. 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. We hope it will be helpful to you in your web automation testing. The code example demonstrates how to use Selenium to search and click on the first result on Baidu. Readers can modify it according to their own needs.

It should be noted that when using Selenium for automated testing, the stability and reliability of the test cases need to be ensured. 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., robust code needs to be written to deal with different situations.

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

Finally, I would like to thank everyone who read my article carefully. Looking at the increase in fans and attention, there is always some courtesy. Although it is not a very valuable thing, if you can use it, you can take it directly!

Software Testing Interview Document

We must study to find a high-paying job. The following interview questions are the latest interview materials from first-tier Internet companies such as Alibaba, Tencent, Byte, etc., and some Byte bosses have given authoritative answers. After finishing this set I believe everyone can find a satisfactory job based on the interview information.
 

Insert image description here

Guess you like

Origin blog.csdn.net/myh919/article/details/133271898