Webdriver in Python3.x:selenium for page element positioning
The elements on the page are like people, with various attributes, such as element name, element ID, element attribute (class attribute, name attribute), and so on. Webdriver are positioned using these attributes of the element.
Common element properties that can be used for positioning:
- Id
- Name
- Class name
- Tag name
- Link text
- Partial link text
- Xpath
- CSS Selector
The methods corresponding to locating an element in Webdriver are:
- Driver.find_element_by_name ()
- DRIVER.FIND_ELEMENT_BY_ID ()
- Driver.find_element_by_class_name ()
- Driver.find_element_by_tag_name ()
- Driver.find_element_by_link_text ()
- Driver.find_element_by_partial_link_text ()
- Driver.find_element_by_xpath ()
- Driver.find_element_by_css_selector ()
The method of locating a set of elements in the corresponding Webdriver:
- Driver.find_elements_by_name ()
- DRIVER.FIND_ELEMENTS_BY_ID ()
- Driver.find_elements_by_class_name ()
- Driver.find_elements_by_tag_name ()
- Driver.find_elements_by_link_text ()
- Driver.find_elements_by_partial_link_text ()
- Driver.find_elements_by_xpath ()
- Driver.find_elements_by_css_selector ()
Examples of Use:
- XPath Positioning
" "XPath is a document localization language. Because HTML can be seen as an implementation of XML, selenium users can use this powerful language to locate in Web applications. Absolute path method: from the root element, when the level of the element is very deep, the path will be long, the reading is not good, it is difficult to maintain. It is not recommended to make an absolute path such a method. Relative path method: Through Firebug very easy to get the relative path of the XPath, open the Firebug plugin, on the page search text box, you can display the XPath;" "#Search by XPath, any element with an id attribute of ' kw 'Driver.find_element_by_xpath ("//*[@id = ' kw ')"). Click ()#Search by XPath, any element that (* represents) the input property is ' kw 'Driver.find_element_by_xpath ("//input[@id = ' kw ')"). Click ()#the current element needs to be positioned according to the attributes of the parent directory#location by ID property of the previous level directoryFind_element_by_xpath ("//span[@id = ' Input-container ']/input") #location by id attribute on level three directoryFind_element_by_xpath ("//div[@id = ' HD ']/form/span/input") #positioned by the Name property on the three level directoryFind_element_by_xpath ("//div[@name = ' Q ']/form/span/input")
Webdriver in Python3.x:selenium for page element positioning