web page element positioning

All web pages there are eight elements in positioning mode

Characterized by a single element to find: six kinds (id, class_name, tag_name, name , link_text (2))
of the various features and elements come relationships: 2 kinds (xpath, css)

 

The following four methods are available for all elements

1.id: single, preferred it when id unchanged.

# When an element has many attributes, positioning element is first priority id, id but sometimes change, if id is constant, it can be used to locate. 
# If the id changes / dynamic, so you want to discard id targeting method, for anything else. 
# Of Baidu home page, for example: 
# Here is the positioning strategy id, positioning expression is id = "kw". 
= driver.find_element_by_id ELE ( " kW " )  
 # element has attributes ----- + operator returns webElement objects (so selenume element is made of a layer of the package) 
# Driver All methods are beginning to find find_element All search results are webElement. 
# The above mentioned id will only be found in the web page in one element, so that only one object can be returned. 
# If you find is more, the development must be corrected. 
Print (ELE)

 

2. Label name tag_name: you can find one or more, is not unique, so find methods are the same.

= driver.find_element_by_tag_name ELE ( " INPUT ")    # matched to the DOM / html first element 
Print (ELE)

on ELES = driver.find_elements_by_tag_name ( " INPUT ")    # plurality, returns a list of elements of the object webElement , pages match all the elements.
# press the entire html page from top to bottom layers to find
Print (ELES)

 

3 . Class_name (not exclusively): Same as above.

ele1 = driver.find_element_by_class_name("")
print(ele1)

ele2 = driver.find_elements_by_class_name("")
print(ele2)
 
 

4.name (not unique): Same as above.

# Although the name very often the same name, but not the same name as far as possible, each person has their own name, nominally absolutely unique, but often the only highly 
ele3 = driver.find_element_by_name
 Print (ELE1) 

ele4 = Driver. find_elements_by_name
 Print (ELE1)

  

a elements: two methods

The complete text value matches

driver.find_element_by_link_text ( " More " )  
 # A link text must be "more products" four words    
# matching full amount, exact match for 
# is not the only absolute: a page that allows multiple link text value is the same as      
# Current page, any element of the text as long as it is 'more', it would be an exact match.

 

6. partial match / comprising

driver.find_elements_by_partial_link_text("产品")  
# Long as the "product" should be the content of a text element, to meet the requirements.

 

6 or more methods, when used, it may sometimes not be found elements, or to find the back of changed again. Therefore, the combination positioning xpath or css.

7. xpath positioning


# Absolute positioning: absolute path (compatible poor, over-reliance on position and inheritance) from our ancestors began to itself begins with / / parent / child
# Relative positioning:
# In the whole html page, one by one condition, to find the only element. 
# Get rid of the position and inheritance, web automation choice.
Press ctrl + f # open field below use a relative path to find
# Plurality of attributes: using logical operations and / or

 1> begins with //, // tag name [@ attribute name = value] * If the label name, all matching labels.

the INPUT // [@ the above mentioned id = " kw " ]   # find property id, the element value kw

2> more attributes: using logical operations and / or

* // [@ the above mentioned id = " kw "  and @ name = " WD "  and @ class = " s_ipt " ]   # meet 
// * [@ the above mentioned id = " kw "  or @ name = " WD "  or @ class = " s_ipt " ] # meet one

3> positioned by text element before fixing // --- text [text value text () = corresponding] tag name in function ----

P // [text () = " average attendance " ]

4> comprising: Attribute / text can comprise a portion of the tag name // ----- [contains (@ property / text (), the contents contained)]

# Attribute mode comprising: 
// span [the contains (@ class , " quickdelete-wrap " )] 
# text mode comprising: // h1 of [the contains (text (), " No. 2 " )]
# comprises logic and / or combining // h1 of [the contains (text (), " No. 2 " ) and @ ID = " CCC " ]

5> level positioning: 

# By parents / ancestors generation 
# to Baidu home page, for example: 
# locate login element 
# it is a a link, link address, with name / class / onclick attribute text. 
The preferred name attribute positioning #  
 // A [@ name = " tj_login " ]
 # two elements will not know which can be placed in each mouse two yellow part, will show location path / element characteristics is looking Elements.

Two elements of the same, impossible to find itself on its own.

Find all matched elements with find elements, then index value.

 

Two identical hard to tell, probably father / fathers are not the same.

By parents / ancestors generation, first find is not the same ancestors, ancestors find in below. ------ level positioning

# Find elements by their parents: 
# / only with lineal descendants (restrict context: parent / child) 
# // as long as the descendants of the line 
// div [@ the above mentioned id = " U1 " ] / A [@ name = " tj_login " ]

6> axis positioning: the table used for

Known elements / :: axis name tag name [@ attribute = value]

Axis operation:

ancestor: ancestor nodes, including the parent.

parent: parent node

preceding: all the nodes before the current node label element. (Html ​​page order)

preceding-sibling: all current sibling nodes preceding an element tag.

following: All the elements of the current node after the node label. (Html ​​page order)

following-sibling: all current sibling node after the label element.


Guess you like

Origin www.cnblogs.com/xcc-/p/11504674.html