Simple entry to seleniumUI automated testing

1. Introduction to selenium

Selenium is a popular automated testing tool for testing the functionality and user interface of web applications. It can simulate user operations in the browser, such as clicking, entering text, selecting drop-down boxes, etc., and verifying the status and properties of page elements. Selenium can help testers automate repetitive testing tasks, improve testing efficiency and reduce human errors.

Second, the principle of selenium

image.png

In our automation program, we need to import the WebDriver class in the selenium library to create a WebDriver object, and send a request to the browser driver (such as ChromeDriver) by calling the corresponding function of the selenium client library.

When we use the WebDriver driver to execute code, WebDriver will convert our instructions into HTTP requests and send them to the browser driver.

The browser driver is a bridge connecting the Selenium library and the browser. It is responsible for communicating with the browser and executing the code we write to control and operate the browser.

After the browser driver receives these HTTP requests, it will convert them into instructions executable by the browser, and send the instructions to the browser for execution. After the execution is completed, the browser returns the result to the browser driver, and the browser driver converts the result into an HTTP response and returns it to our code program.

Three, selenium's eight element positioning methods

The key to selenium's UI automation is element positioning. It can be said that as long as the element is positioned, the method can be called to manipulate the UI element. Before introducing the method of element positioning, we need to know whether our positioning code is written correctly. At this time, the power of F12 is reflected!

You can bring up the developer tools, select Elements, click on an element at will, and then click Ctrl+F. In this search box, you can search all the elements on the current page, and then copy the code we locate the element to search. If the search result can If the element is located, it proves that the method of locating the element is written correctly.

image.png

In selenium, there are eight methods of element positioning, which will be introduced below.

1. ID positioning:

find_element(By.ID, 'Id属性值')

Example:image.png

If the id attribute value is kw, the code isfind_element(By.ID, 'kw')

In CSS, id is an important attribute used to identify web page elements and id is unique. Therefore, if the element to be located has an id value, locating by ID is a very convenient way.

2. Name positioning:

find_element(By.NAME,'name属性值')

Example:

image.png

If the name attribute value is referrer, the code isfind_element(By.NAME,'referrer)

3. Class positioning:

find_element(By.CLASS_NAME,'class属性值')

Example:

image.png

The class attribute value is foot-async-script, and the code isfind_element(By.CLASS_NAME,'foot-async-script')

When there are multiple class attribute values, you can specify any class attribute value, and you can select this element

4. Tag positioning:

find_element(By.TAG_NAME,'元素的标签值')

Example:

image.png

If the tag value is style, the code isfind_element(By.TAG_NAME,'style')

Because the located element is not unique, we can use it find_elements(By.TAG_NAME,'style'), pay attention to the difference between find_element and find_elements, find_elements returns a list

5. link_text positioning:

find_element(By.LINK_TEXT,'超链接文字')

Example:

image.png

For example, the video on Baidu's homepage is a hyperlink text, and the code can be written asfind_element(By.LINK_TEXT, '视频')

6. Partial_link_text positioning:

find_element(By.PARTIAL_LINK_TEXT,'超链接部分文字')

For example,

image.png

If the text of the hyperlink is too long, you can only enter part of it, and you can also locate the element. As shown in the figure, the attribute value of "Baidu, you will know-Mobile Homepage" can be written as: , code: to locate 移动首页the find_element(By.PARTIAL_LINK_TEXT, '移动首页')element

7. css positioning:

find_element(By.CSS_SELECTOR,'css值')

More often, elements do not have id, class values, or attribute values ​​are not unique. At this time, we can use CSS selector syntax to select elements. CSS selector is a method commonly used in web development to locate elements, which can be found in Quickly and accurately find the desired element in an HTML document.

(1) Select elements by tag name

For example, tags such as div, p, a, etc., for example, find all elements whose tag is named div

find_elements(By.CSS_SELECTOR, 'div')

(2) Select elements by class name class

The syntax is to add a sign before the class value .,

Example:

image.png

The code is:find_element(By.CSS_SELECTOR, '.foot-async-script')

(3) Select elements by ID

The syntax is to add a sign before the id value #,

Example:

image.png

The code is:find_element(By.CSS_SELECTOR, '#result_tts_player')

(4) Select elements through attributes

The syntax is [attribute name=attribute value]

Example:

image.png

The code is: find_element(By.CSS_SELECTOR, '[name=theme-color]')it can also be used in combination with tag names, id values ​​or class names such asfind_element(By.CSS_SELECTOR, 'meta[name=theme-color]')

(5) Select by sub-element

grammar:元素1 > 元素2

If element 2 is inside the element, such as:

image.png

The class attribute wgt-navbar is under the class attribute navbar-wrapper level, you can use the sub-element syntax to locate the element more accurately, the code is:find_element(By.CSS_SELECTOR, '.navbar-wrapper > .wgt-navbar)

(6) Select by descendant elements

grammar:元素1 元素2

Like child elements, descendant elements are also inside elements; the difference is that child elements can only be direct child elements of the element, and descendant elements may not be direct child elements, child elements must be descendant elements, and descendant elements are not necessarily child elements .

For example, element a > element b > element c > element d, element b is a child element of element a, element c is a child element of element b but not a child element, and element c is a descendant element of element a.

Example:

image.png

The class attribute navbar-bg is under the class attribute wgt-navbar level, and wgt-navbar is a child element of navbar-wrapper, then navbar-bg is a descendant element of navbar-wrapper. Then you can use the descendant element syntax to locate the element more accurately, the code is:find_element(By.CSS_SELECTOR, '.navbar-wrapper .navbar-bg)

8. XPath positioning

The advantage of XPath positioning is that it is flexible and powerful, and it can be positioned according to multiple characteristics such as element attributes, tag names, hierarchical relationships, and text content. In some cases, XPath targeting may be more suitable for locating complex element structures or special elements. However, XPath expressions are more complex than CSS selectors and can sometimes be more verbose. Therefore, in actual use, you can choose to use CSS selectors or XPath positioning to locate elements according to specific situations.

XPath positioning methods can be divided into absolute path and relative path.

(1) Absolute path positioning:

Absolute paths start from the root node and go through a series of node paths to locate elements. The absolute path starts with a slash /, indicating the root node, and then locates step by step according to the node hierarchy. For example, /html/body/div[1]/input means starting from the root node, first select the html element, then select the body element, then select the first div element, and finally select the input element.

(2) Relative path positioning:

A relative path is a path relative to the current node, which is more commonly used and flexible. A relative path starts with a double slash //, which means that it starts from any position of the current node and selects elements that meet the conditions. For example, //input means to select all input elements in the document, regardless of their position in the document. The following describes how to locate elements based on features such as element attributes, tag names, hierarchical relationships, and text content.

//tagname : Select all elements with the specified tag name

//tagname[@attribute='value'] : Select elements with the specified attribute and attribute value.

// [@attribute='value'] * : Select any element with the specified attribute and attribute value

//parent/child : Select the direct child elements under the parent element.

//ancestor/descendant : Select all descendant elements under the ancestor element.

// [text()='value'] * : Select any element with the specified text content.

The XPath positioning method also supports positioning using logical operators and multiple conditions. For example, you can use and, or, not to combine multiple conditions.

Fourth, common element operations of selenium

click(): Click on the element.

send_keys(value): Send text to the input box element.

clear(): Clear the text of the input box element.

get_attribute(name): Get the specified attribute value of the element.

is_displayed(): Determine whether the element is visible.

is_enabled(): Determine whether the element is available.

is_selected(): Determines whether the element is selected.

Five, examples

Requirement: Enter Weibo from Baidu search, then obtain Weibo hot search data and output it to a txt file

python
复制代码
import time
from selenium import webdriver
from selenium.webdriver.common.by import By

class test(object):
   def __init__(self):
       self.driver = webdriver.Chrome()
       self.driver.get('https://www.baidu.com/')
       #self.driver.get('https://weibo.com/')
       self.driver.implicitly_wait(10)      #`全局等待` 该方法接受一个参数, 用来指定最大等待时长
       self.driver.maximize_window()        #最大化浏览器页面
   def baidu_search(self):
       self.driver.find_element(By.ID,'kw').send_keys('微博')   #通过id值定位
       self.driver.find_element(By.CSS_SELECTOR,"[value=百度一下]").click() #通过CSS选择器定位
       self.driver.find_element(By.PARTIAL_LINK_TEXT,'微博-随时随地发现新鲜事').click()  #通过超链接定位
       for handle in self.driver.window_handles:   #不断切换窗口至我们想要的窗口:Sina
           # 先切换到该窗口
           self.driver.switch_to.window(handle)  
           # 得到该窗口的标题栏字符串,判断是不是我们要操作的那个窗口
           if 'Sina' in self.driver.title:
               # 如果是,那么这时候WebDriver对象就是对应的该该窗口,正好,跳出循环,
               break
   def get_weibo_hot_search(self):
       time.sleep(10)
       hot_search_list = self.driver.find_elements(By.XPATH, '//*[@class="wbpro-textcut f14 cla"]')
       print(len(hot_search_list))
       print(hot_search_list)
       for list in hot_search_list:
           hot_search_text=list.text
           self.save_data(hot_search_text)
   def save_data(self, hot_search_text):
           with open(r'C:\Users\User\Desktop\output.txt', 'a', encoding='utf-8', newline='') as f:  # 用追加的方式将数据保存到txt文件中
              # for number,hot_search_text in hot_search.items():
               f.write(hot_search_text+'\n')
   def main(self):
       self.baidu_search()
       self.get_weibo_hot_search()
       self.driver.quit()

if __name__ == '__main__':
   ceshi = test()		# 实例化对象
   ceshi.main()		# 调用类函数入口

Digression

In this first year of fast-growing technology, programming is like a ticket to a world of infinite possibilities for many people. In the star lineup of programming languages, Python is like the leading superstar. With its concise and easy-to-understand syntax and powerful functions, it stands out and becomes one of the most popular programming languages ​​in the world.


The rapid rise of Python is extremely beneficial to the entire industry , but " 人红是非多" has caused it to add a lot of criticism, but it still cannot stop its hot development momentum.

Will Python remain relevant and intact for the rest of the next decade? Today, we're going to analyze the facts and dispel some misconceptions.

If you are interested in Python and want to get a higher salary by learning Python, then the following set of Python learning materials must be useful to you!

Materials include: Python installation package + activation code, Python web development, Python crawler, Python data analysis, artificial intelligence, machine learning and other learning tutorials. Even beginners with 0 basics can understand and understand. Follow the tutorial and take you to learn Python systematically from zero basics!

1. Learning routes in all directions of Python

The route of all directions in Python is to organize the commonly used technical points of Python to form a summary of knowledge points in various fields. Its usefulness lies in that you can find corresponding learning resources according to the above knowledge points to ensure that you learn more comprehensively.
insert image description here
2. Python learning software

If a worker wants to do a good job, he must first sharpen his tools. The commonly used development software for learning Python is here!
insert image description here
3. Python introductory learning video

There are also many learning videos suitable for getting started with 0 basics. With these videos, you can easily get started with Python~insert image description here

4. Python exercises

After each video lesson, there are corresponding practice questions, you can test the learning results haha!
insert image description here

Five, Python actual combat case

Optical theory is useless. You have to learn to type codes along with it, and then you can apply what you have learned in practice. At this time, you can learn from some practical cases. This information is also included~insert image description here

6. Python interview materials

After we have learned Python, we can go out and find a job with the skills! The following interview questions are all from first-line Internet companies such as Alibaba, Tencent, and Byte, and some Alibaba bosses have given authoritative answers. After reading this set of interview materials, I believe everyone can find a satisfactory job.
insert image description here
insert image description here
7. Information collection

The full set of learning materials for the above-mentioned full version of Python has been uploaded to the CSDN official website. Those who need it can scan the QR code of the CSDN official certification below on WeChat to receive it for free.

Guess you like

Origin blog.csdn.net/Python966/article/details/132321867