A simple location method (these attributes of an element are globally unique)
- The id attribute
- The name attribute
- The class name property
- Link_text Hyperlink text location
- Partial_link_text The part that matches the hyperlink text
- Tag_name (but common tag names are globally unique and not commonly used)
Above we take Baidu as an example
Open Browser https://www.baidu.com/ chrome
Input TEXT KW RobotFramework # Id attribute
Press keys wd \ue007 #
Second XPATH location
XML Path Language (xpath) is a language that searches for information in XML documents. It can be used to traverse elements and attributes in XML documents. Xpath location is commonly used in automated tests
(1) The absolute path starts with an HTML tag, which is generally deprecated
For example: / HTML/body/div/div # / input/span span to the elements
/ HTML /body/div[2]/div/div[2]/div[1]/form/input[2] # Find the second input element in the form tag
(2) The relative path starts with ‘//’. The following are common paths
-
Tags combine attributes: //tag[@attribute= “Value”] //input[@class=’but1′]” Find the input tag for class=but1
-
Boolean logic operation; //tag[@attribute1= “Value1” and @attribute2= “Value2”] // Input [@class=’but1’and @name=’key’]
-
Fuzzy matching: (1) Some of the attribute values are always unchanged, // Tag [contains(@attribute1, “Value1”)] // Tag [end-with(@attribute1, “Value1”)] //a[starts-with(@class,’abc123′)] //a[end-with(@attribute1, ‘abc123’)] “Value1”)] / / a [end – with (@ class, ‘end’)]
-
Text matching, the text can also use fuzzy matching / / tag/text () = “value”] / / p/text () = “hello”] / / tag [contains (text (), “value”)]
-
The parent node locates the child node
//tag1/tag2[index] //tag1[@attribute= “Value”]/tag2
-
* Matches //*[@attribute= “value”]/tag2
-
Preceding – SIBING :: INPUT/TAG1 //parent::tag2 // SPAN [@icon-name=”error-line”]/preceding- SIBing :: INPUT
-
Preceding -sibling:: tag1//preceding-sibling::tag2
-
Tag1 //following-sibling::tag2
Three CSS positioning
(1) The absolute path HTML >body>div>input, the element subscript also starts at 1 HTML >body>div[3]
(2) Relative path
tag.class-value
.class-value
input.but1
.but1
The class attribute
tag#id-value
#id-value
i#cart_num
#cart_num
The id attribute
The tag/attribute = “value”
Input [placeholder=' placeholder ']
Other attributes
Element location methods use summary:
-
First consider ID localization, which is the most efficient.
-
If you do not have an ID, choose XPath and try to use relative paths.
-
Tag_name is the least frequently used.
-
Try not to use href attributes, pure number attributes (pure number may be a dynamic value) to locate.
Element cannot be located cause:
-
The wait time is insufficient, the element is not loaded, add the wait time.
-
Multiple Windows, the window handle is still in the previous window, so the element of the new window cannot be located, and the window handle needs to be changed.
-
Iframe/Frame cause: This is the most common cause. A frame is actually embedded in another page, and WebDriver can only identify one page at a time. Therefore, it is necessary to locate the corresponding frame first and then locate the elements in that page.
-
The locating method is incorrect.