When doing UI automation of small programs, the most common and basic problem that everyone encounters is how to identify or locate the elements that need to be operated.
For Minium's documentation on element operations, please refer to Element Positioning . Here we introduce several methods of element positioning in detail.
Since the applet is a Web-like method, when you do element selection, you can first understand some basic knowledge of CSS selectors, which can be referred to
Minium can locate elements through WXSS selectors, which generally have the following types:
- Selector selector (id/class/label/attribute, etc.)
special class, id and other attribute selector positioning, for example
<view class="child">test</view>
- selector selector + inner_text/text_contains/value
element attribute or label positioning and text positioning within the element, for example
<view class="child">test</view>
<view>test2</view>
- XPath positioning
Get element XPath in the developer tool, you can right-click to selectCopy
, clickCopy Xpath
orCopy full Xpath
- Descendant selectors across custom components (use >>> to connect custom component elements)
<test22>
<view>xxx</view>
</test22>
Recommended usage of various positioning methods
- xpath: It is recommended to use the complete xpath(
full Xpath
) path without writing such as "//", which helps to improve the search speed - selector: recommended
id/class/标签+属性
.inner_text/text_contains/value
In order to enhance the usage, the essence of the implementation is to get the elements through the selector and then filter the elements through inner_text/text_contains/value - Method for obtaining elements across custom components: It is recommended to use xpath. The implementation of the selector method needs to obtain the custom component first and then find its sub-components that meet the conditions
The positioning method of the built-in components of the applet
For example, the applet page wxml
1. Position the second element class="child"
of
Several writing methods of minium script
# 1. 耗时较短
get_element("/page/view[6]/view") # xpath
# 2.耗时较长
ele = get_elements("view[class='child']")[1] # 返回第2个class=child的元素
# 3.耗时最长
get_element(".child",inner_text="parent>child") # class=child,控件内文本为parent>child
2. Position the first element class="testclass"
of
Minium script writing
1.耗时较短
get_element("/page/view[2]") # xpath
2.耗时较长
get_element("view[class='testclass']") # 返回第一个class=testclass的元素
3.耗时最长
get_element(".testclass",inner_text="test class 2") # class=testclass,控件内文本为test class 2
3. Locating elements without attributes
Minium script writing
1.耗时较短
get_element("/page/view[1]") # xpath
2.耗时较长
get_element("view",inner_text="first node") # view标签,控件内文本为first node
Positioning methods across custom component elements
How to identify custom components
- Look at the wxml file or the wxml panel of the WeChat developer tool, the tag names are listed in the
不在
official component list of the applet自定义组件
- Look at the wxml panel of the WeChat developer tool, if there
#shadow-root
is , it is自定义组件
. For example, in the applet page wxmlmytest
,test2
,test22
1. test2
Locate text
the element under the tag
Minium script writing
1.耗时较短
get_element("/page/mytest//test2//view/view/text") # xpath
2.耗时较长
get_element("mytest>>>test2>>>text") # 自定义组件 mytest组件节点下的test2组件节点下text标签
3.耗时最长
# 逐个获取自定义组件及自定义组件下的标签
get_element("mytest").get_element("test2").get_element("#test2").get_element("text")
2. test22
Locate the first view
element under the tag
Minium script writing
1.耗时较短
get_element("/page/mytest//test22/view") # xpath
2.耗时较长
get_element("mytest>>>test22>>>view") # 自定义组件 mytest组件节点下的test2组件节点下text标签
3.耗时最长
# 逐个获取自定义组件及自定义组件下的标签
get_element("mytest").get_element("test22").get_element(".test22")
Summarize
How to reduce element lookup time
For example, the method of obtaining elements in this use case, the search is slow, which will lead to a long execution time of the use case
get_element("view", inner_text="xxx", max_timeout=5)
Element positioning uses the basic label view and text methods, and the time is relatively long. It is recommended to use special class and other css selectors for positioning , or try to use xpath (//view)
Choose a targeting method
- If the page structure of the applet does not change frequently, XPath positioning can be used first
- For some projects, after the project is packaged, the element class, id and other attributes will change randomly. Consider using XPath positioning, or label + text positioning
- If the element class, id and other attributes will not change randomly and have certain identification, selector positioning or XPath positioning can be considered
- If the element has no attributes, you can consider XPath, or label + text positioning
- Custom component positioning prioritizes XPath over descendant selectors across custom components