Selenium 2 7 automated testing actual (positioning element)

A, xpath positioning

1. absolute positioning
of example with Baidu input box and a search button
EG: find_element_by_xpath ( "/ HTML / body / div / div [2] / div / div / div / from / span / INPUT")

EG: find_element_by_xpath ( "/ HTML / body / div / div [2] / div / div / div / from / span [2] / the INPUT ")
Find_element_by_xpath () method uses xpath language to locate elements, xpath primarily hierarchical relationship tag name to locate elements of an absolute path , the outermost layer of the html language. Text in the body, looking down to a level, if there are a plurality of the same label name next level, then it is determined by the vertical sequence of several, eg: div [2] represented by the current level of the second div tag

2 using the positioning element attributes
except for using an absolute path outside, the Xpath element attribute values may be used to locate, likewise Baidu input box and a search button Example:
find_element_by_xpath ( "// iNPUT [@ ID = 'kW']" )
find_element_by_xpath ( "// the INPUT [@ id = 'su']")
// current page a directory, input indication label name position elements, [@ id = 'kw' ] represents the value of the id attribute of this element equal kw. Below positioned by name and class attribute value.
find_element_by_xpath ( "// INPUT [@ name = 'WD']")
find_element_by_xpath ( "

find_element_by_xpath ( "// * [@ class = 'bg_s_btn']")
If you do not want to specify the tag name, you can also (*) are replaced with asterisks. Of course, xpath not limited id, name, class three attribute values. Any attribute value of the element can be used, as long as it can uniquely identify an element.

3. The level combined with the property
if the element does not have a property value that uniquely identifies this element, we can find it on one element, if it's on a value that uniquely identifies the elements can have attributes, and can also be use.
EG: find_element_by_xpath ( "// span [@ class = 'bg_s_ipt_wr'] / input")
span [@ class = 'bg_s_ipt_wr'] to the parent element is positioned by the class attribute, back / input on the parent element represented by the following sub-elements. If the parent element has no attributes values available, we can continue to look up "Grandpa" element. We can find a method by which an upward until you find the outermost <html> tag, which is an absolute path of the wording.

4. Logical operators
if not a unique property to distinguish one element, may also be connected to a plurality of logical operators to find the element properties.
#and
find_element_by_xpath ( "// INPUT [@ ID = 'kW' class and @ = 'SU'] / span / INPUT")

 

Two, CSS positioning


1. CSS syntax and usage

 

 

2. Location class attribute:

find_element_by_css_selector (. "s_ipt")
find_element_by_css_selector () method is used to locate the CSS language elements, the dot (.) denotes the class attribute element positioned by


3. By positioning the id attribute:
find_element_by_css_selector ( "# kW")
pound sign (#) is positioned by id attribute element


4. By positioning the tag name:
find_element_by_css_selector ( "the INPUT")
label name duplication probability is very large, it is difficult to find the elements you want in this way.


The parent-child relationship by positioning:
find_element_by_css_selector ( "span> input")
father elements span, look for its child elements named input of all labels.


6. By locating property:
find_element_by_css_selector ( "[name = 'kW']")
find_element_by_css_selector ( '[type = "Submit"]')
may be used in any of the CSS properties of the elements, as long as these attributes can uniquely identify the element. For the attribute values, it may be quoted, may not be added, but note quotation mark to distinguish the entire string.


7. The composition Location:
find_element_by_css_selector ( "span.bg s_ipt_wr> input.s_ipt")
there is a parent element, its label named span: It has a class attribute value called bg s_ipt_wr: it has a child element, the tag name input and the class attribute value of this sub-element called s_ipt.

 

Third, a similar function xpath contrast with the CSS

 

 


1 with the positioning element By
webdriver also provides another way to write, i.e. find_element unified call () method, to declare By positioning method, and positioning parameters corresponding to the incoming positioning method, as follows:

 

 

find_element () method is used only for locating elements, he takes two parameters, the first parameter is a type of positioning, by By; DETAILED second parameter is located. By introducing the required class By prior to use.
from selenium.webdriver.common.by import By

Guess you like

Origin www.cnblogs.com/Rita-LJ/p/11528220.html