Turn from: http://www.cnblogs.com/qingchunjun/p/4208159.html
By.xpath ()
This method is a very powerful way to find elements, which can be used to locate almost any element on the page. Before we can formally start using XPath for positioning, let's take a look at what XPath is. XPath is the abbreviation for XML path, because the HTML document itself is a standard XML page, so we can use XPath syntax to locate page elements.
Suppose we now take the HTML code shown in figure (2) as an example, to refer to the corresponding object, the XPath syntax is as follows:
Figure (2)
Absolute path notation (only one), as follows:
Reference the form element on the page (that is, line 3rd in the source code):/html/body/form[1]
Note: 1. The XPath absolute path of an element can be queried directly by Firebug. 2. The use of absolute path is generally not recommended, because once the page structure changes, the path will also expire, you must write again. 3. The absolute path is represented by a single/number, and the relative path below is to//indicate that the difference is very important. Another thing to say is that when the path of XPath begins with/begins, the XPath parsing engine is parsed from the root node of the document. When the XPath path starts with//, it means that the XPath engine parses from any conforming element node of the document. And when/appears in the XPath path, represents a direct child node looking for the parent node, and when//appears in the XPath path, it means looking for any eligible child nodes under the parent node, regardless of how many levels are nested (these are examples below, which you can refer to for experimentation). To figure out this principle, you can understand that the XPath path can be combined with absolute paths and relative paths to show how you want to play.
The following is a reference to the relative path:
Find page root element://
Find all input elements on a page://input
Finds the direct child INPUT element within the first FORM element on the page (that is, only the next-level INPUT element that includes the form element, using an absolute path representation, single/number)://form[1]/input
Find all the child input elements within the first FORM element on the page (as long as the input in the form element is counted, regardless of how many other tags are nested, using a relative path representation, double//)://form[1]//input
Find the first FORM element on a page://form[1]
Find the form element with ID loginform on the page://form[@id = ' loginform ']
Find the INPUT element with the name attribute username on the page://input[@name = ' username ']
Finds the first INPUT element under the form element with ID loginform on the page://form[@id = ' loginform ']/input[1]
Lookup page has an INPUT element with the Name property Contiune and the type attribute to button://input[@name = ' Continue '] [@type = ' button ']
Find the 4th INPUT element under the form element with ID loginform on the page://form[@id = ' loginform ']/input[4]
XPath is powerful, so it can be written more complicated, like the HTML source code of Figure (3) below.
Figure (3)
If we're going to refer to the INPUT element with ID "J_password" Now, how do we write it? We can write this as follows:
webelement password = driver.findelement (By.xpath ("//*[@id = ' J_login_form ']/dl/dt/input[@id = ' J_password ']"));
Can also be written as:
webelement password = driver.findelement (By.xpath ("//*[@id = ' J_login_form ']/*/*/input[@id = ' J_password ']"));
Here's an explanation, where//*[@id = ' j_login_form '] refers to finding an element under the root element with any ID of j_login_form, which is equivalent to referring to the form element. The following path must follow the source hierarchy and write down. According to the code shown in figure (3), the INPUT element we are looking for is contained within a DT tag, and DT is included in the DL tag, so the middle must be written with a DL and a DT two layer before the input layer. Of course we can also use the * to omit the specific label name, but the level of the elements of the relationship must be reflected, such as we can not write//*[@id = ' J_login_form ']/input[@id = ' J_password '], this will certainly be an error.
All of the foregoing are based on the positioning of the exact element attributes in XPath, and in fact XPath can also be used as a positioning artifact for fuzzy matching. For example, the following figure (4) shows the code:
Figure (4)
The "Exit" hyperlink in this code has no standard ID element, only one rel and href, not very well positioned. We can use some of the XPath pattern of fuzzy matching to locate it, there are three main ways, such as the following.
A. Using the CONTAINS keyword, locate the code as follows:
1 driver.findelement (By.xpath ("//a[contains (@href, ' logout ')"));
The idea is to find that the HREF attribute value in the page contains all a elements of the word logout, because the href attribute of the Exit button will certainly contain logout, so this method is feasible and often used. Where the @ can be followed by any property name of the element.
B. With Start-with, locate the code as follows:
1 driver.findelement (By.xpath ("//a[starts-with (@rel, ' Nofo ')"));
The meaning of this sentence is to look for a element whose rel attribute begins with Nofo. The rel behind the @ can be substituted for any other attributes of the element.
C. With the text keyword, locate the code as follows:
1 driver.findelement (By.xpath ("//*[text () = ' exit ')");
This method is quite domineering ah. Find out all the exits in the page without knowing it is a element. This method is also often used for plain text lookup.
In addition, if you know the text content of a hyperlink element, you can also use the
1 driver.findelement (By.xpath ("//a[contains" (Text (), ' exit ')));
This method is commonly used when you know some or all of the text information displayed on a hyperlink.
Finally, with regard to XPath as a way of positioning, Webdriver will scan all elements of the entire page to locate the elements we need, so this is a very time-consuming operation, and if you use XPath to do element positioning in your script, it will cause your script to perform much less quickly. So please use caution.
By.cssselector ()
Cssselector is similar to XPath, but executes faster, and the various browsers support it fairly well, so the functionality is pretty powerful.
Here are some common ways to locate Cssselector:
A DIV element that locates an ID of Flrs, can be written as: #flrs Note://div[@id equivalent to XPath syntax = ' flrs ']
The position ID is a element under Flrs and can be written as #flrs > a note://div[@id equivalent to XPath syntax = ' flrs ']/a
The value of the href attribute with the location ID flrs is/forexample/about.html, which can be written as: #flrs > a[href= "/forexample/about.html"]
If you need to specify multiple attribute values, you can add them one at a time, such as #flrs > input[name= "username"][type= "text".
After we understand the basic syntax, we try to use the Cssselector method to refer to the input object selected in Figure (3) with the following code:
webelement password = driver.findelement (By.cssselector ("#J_login_form >dl>dt>input[id= ' J_password ')");
Also must pay attention to the level relations, this cannot omit.
Cssselector is also useful for locating elements that use a composite style sheet, previously mentioned in the 4th way classname. Now let's see how we can use Cssselector to refer to the button mentioned in the 4th way. The button code is as follows:
<button id= "J_sidebar_login" class= "btn btn_big btn_submit" type= "submit" > Login </button>
The Cssselector reference element code is as follows:
Driver.findelement (By.cssselector ("Button.btn.btn_big.btn_submit"))
。 This allows you to successfully reference elements that use a composite style.
In addition, Cssselector also has some advanced usage, if skilled can more easily help us locate elements, such as we can use ^ to match a prefix, $ for matching a suffix, * used to match any character. For example:
Matches an id attribute, and the ID property is a hyperlink element that starts with "id_prefix_": a[id^= ' id_prefix_ '
Matches an id attribute, and the ID property is a hyperlink element that ends with "_id_sufix": a[id$= ' _id_sufix '
Matches a hyperlink element that has an id attribute and contains the "Id_pattern" character in the id attribute: a[id*= ' Id_pattern '
Finally, a summary of the various ways in the choice of time should be how to choose:
1. When the page element has id attribute, it is best to use ID to locate. But because many programmers in real projects actually write code that is not standard, there are many criteria that are missing, then only other positioning methods are selected.
2. XPath is tough, but the positioning performance is not very good, so try to use less. If it's true that a few elements are not positioned properly, you can choose XPath or cssselector.
3. You can consider using tagname or name when you want to locate a group of elements with the same elements.
4. When there is a link need to locate, you can consider Linktext or Partiallinktext mode.
Website:
Http://selenium-python.readthedocs.io/locating-elements.html
Locating by XPath
The XPath is the language used to locating nodes in XML document. As HTML can be a implementation of XML (XHTML), Selenium users can leverage this powerful language to target elements in Their web applications. XPath extends beyond (as as and as supporting) the simple methods of locating by ID or name attributes, and opens up all SOR TS of new possibilities such as locating the third checkbox on the page.
One of the main reasons for using XPath are when you don ' t have a suitable ID or name attribute for the element you wish to Locate. can use XPath to either locate the element in absolute terms (not advised), or relative to a element that does have a N ID or name attribute. XPath Locators can also is used to specify elements via attributes, other than ID and name.
Absolute XPaths contain the location of all elements from the root (HTML) and as a, are to likely with the Slightest adjustment to the application. By finding a nearby element with an ID or name attribute (ideally a parent element) can locate your target element bas Ed on the relationship. This is very less likely to change and can make your tests more robust.
For instance, consider this page source:
The form elements can is located like this:
Login_form = Driver.find_element_by_xpath ("/html/body/form[1]")
Login_form = Driver.find_element_by_xpath ("// FORM[1] "
login_form = Driver.find_element_by_xpath ("//form[@id = ' loginform '] ")
Absolute path (would break if the HTML is changed only slightly) the "form element with ATT" in the HTML Ribute named ID and the value loginform
The username element can be located like this:
Username = Driver.find_element_by_xpath ("//form[input/@name = ' username ']")
username = Driver.find_element_by_ XPath ("//form[@id = ' loginform ']/input[1]")
username = Driver.find_element_by_xpath ("//input[@name = ' username ')" ")
The "I" and "" "child element with attribute named name and the Valueusername the f the form element with attribute named ID and the value loginform the-i-INPUT element with attribute named ' Name ' and ' Value username
The "clear" button element can be located like this:
Clear_button = Driver.find_element_by_xpath ("//input[@name = ' Continue '] [@type = ' button ']")
Clear_button = Driver.find_element_by_xpath ("//form[@id = ' loginform ']/input[4]")
Input with attributes named name and the value continue and attribute named type and the ValueButton fourth input child ele ment of the form element with attribute named ID and value LoginForm