A simple location method (these attributes of an element are globally unique)

  1. The id attribute
  2. The name attribute
  3. The class name property
  4. Link_text Hyperlink text location
  5. Partial_link_text The part that matches the hyperlink text
  6. 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

  1. Tags combine attributes: //tag[@attribute= “Value”] //input[@class=’but1′]” Find the input tag for class=but1

  2. Boolean logic operation; //tag[@attribute1= “Value1” and @attribute2= “Value2”] // Input [@class=’but1’and @name=’key’]

  3. 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’)]

  4. Text matching, the text can also use fuzzy matching / / tag/text () = “value”] / / p/text () = “hello”] / / tag [contains (text (), “value”)]

  5. The parent node locates the child node

    //tag1/tag2[index] //tag1[@attribute= “Value”]/tag2

  6. * Matches //*[@attribute= “value”]/tag2

  7. Preceding – SIBING :: INPUT/TAG1 //parent::tag2 // SPAN [@icon-name=”error-line”]/preceding- SIBing :: INPUT

  8. Preceding -sibling:: tag1//preceding-sibling::tag2

  9. 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:

  1. First consider ID localization, which is the most efficient.

  2. If you do not have an ID, choose XPath and try to use relative paths.

  3. Tag_name is the least frequently used.

  4. Try not to use href attributes, pure number attributes (pure number may be a dynamic value) to locate.

Element cannot be located cause:

  1. The wait time is insufficient, the element is not loaded, add the wait time.

  2. 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.

  3. 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.

  4. The locating method is incorrect.