Objective
Rumors, the martial arts in the spread of eight kinds of positioning, which XPath is Blades Dragon, CSS is leaning day sword.
In addition to these eight kinds, in fact there are 10 kinds of positioning methods, see quickly lost, today's small series to let the long-lost positioning method to re-out of the lake!
18 or 18 Methods of locating
The first eight kinds of people are familiar with, often use
1.id positioning: find_element_by_id (self, id_)
2.name positioning: find_element_by_name (self, name)
3.class positioning: Find_element_ By_class_name (self, name)
4.tag positioning: find_element_by_tag_name (self, name)
5.link positioning: Find_element_by_link_text ( Self, link_text)
6.partial_link positioning find_element_by_partial_link_text (self, link_text)
7.xpath positioning: find_element _by_xpath (self, XPath)
8.css positioning: find_element_by_css_selector (self, css_selector)
These eight kinds are plural
9. ID Complex position find_elements_by_id (self, id_)
10.name complex position find_elements_by_name (self, name)
11.class complex position Find_ Elements_by_class_name (self, name)
12.tag complex position find_elements_by_tag_name (self, name)
13.link complex position Find_ Elements_by_link_text (self, text)
14.partial_link complex position find_elements_by_partial_link_text (self, link_text)
15.xpath Complex position find_elements_by_xpath (self, XPath)
16.css complex find_elements_by_css_selector (self, css_selector
These two are the fast-lost
find_element (self, by= ' id ', value=none)
find_elements (self, by= ' id ', value=none)
Second, element and elements silly confused
The 1.element method is positioned to be singular and is directly anchored to the element
The 2.elements method is a complex number, and this learned English knows that a set of elements is positioned to return a list queue
3. You can view data types with the type () function
4. Print the returned content and see what's different.
Three, elements localization method
1. The previous article has talked about the use of find_element (), see here:
2. Here is an introduction to how to locate elements using the elements method, when there are multiple attributes of the same element on a page, then the parent element's properties are also blurred and not well positioned.
This time do not be afraid, change a thought, do not always think of a positioning, you can first find the same attributes of the elements, take corresponding to the first few on it.
3. For example, Baidu page has six class-like elements, I want to locate the "map" this element
4. Take the corresponding subscript to locate the
Iv. Reference Code
# Coding:utf-8
From selenium import Webdriver
Driver = Webdriver. Firefox ()
Driver.get ("http://www.baidu.com")
# here is a single ID for positioning
element = driver.find_element_by_id ("kw")
Print type (Element)
Print element
# This location is multiple class
elements = Driver.find_elements_by_class_name ("Mnav")
Print type (elements)
Print elements
# The CSS syntax used here
s = driver.find_elements ("CSS selector", ". Mnav")
# ' map ' in fourth position
Print S[3].text
S[3].click ()
# QQ Group: 232607095
# That's a yes, too.
# driver.find_elements ("CSS selector", ". Mnav") [3].click ()
Selenium2+python Automation 45-18 Positioning methods (find_elements) "Reprint"