Tags:--NTB Element Object Smart Note Benefits Union size Child
Tell you, you always rely on that firebug,chrome F12 what right click Copy XPath absolutely one day on the mine bombing you are not able to locate, this time you need to learn to write XPath, the human brain is smarter than the computer, and began to learn the XPath grammar to me!
1th method: Positioning by absolute path (I'm sure you won't use this way)
2nd method: Positioning by Element index
3rd method: Use the XPath property to locate
By.xpath ("//input[@id = ' kw1 ']")
By.xpath ("//input[@type = ' name ' and @name = ' kw1 ']")
4th method: Use partial attribute value matching (the most powerful method)
By.xpath ("//input[starts-with (@id, ' nice ')
By.xpath ("//input[ends-with (@id, ' very pretty")
By.xpath ("//input[contains (@id, ' so Beautiful ')]")
Starts-with, as the name implies, matches the keyword of a property's starting position.
Contains matches a string that is contained in a property value.
Text () matches the display of textual information, which can also be used for positioning purposes.
Input[starts-with (@name, ' name1 ')] Find the page element that contains the ' name1 ' keyword at the beginning of the Name property
Input[contains (@name, ' na ')] find the page element in the name attribute that contains the NA keyword
<a href= "http://www.baidu.com" > Baidu search </a>, then the XPath notation is//a[text () = ' Baidu search '] or//a[contains (text (), "Baidu Search")]
"Part Two" one of my colleagues stepped on a pit when using selenium.
Last time I had a colleague, positioning elements, using the Firefox Firebug tool, navigate to this HTML code:
<span onlick= "88_da_33_699999_x64_3.01.3730.spkg" name= "delete"/> about this, dead and alive, and then using the XPath property of the side-by-side relationship, and the keyword, to the location.
*[contains (@onclick, ' x64_3.01.3730.spkg ') and @name = ' delete ']
"Part Three" several ghosts encountered while switching IFRAME
(1) When switching ifame, sometimes the IFRAME contains ID, you can directly locate the use, but when the IFRAME does not have ID information, such as,
Of course, the actual situation will encounter no id attribute and the Name property is empty, this time it is necessary to locate the iframe element object, which can be positioned through the tag first, but also achieve the same effect.
The following code:
iframe = Driver.find_element_by_tag_name ("iframe")
After switching, it is possible to locate the elements inside the IFRAME normally, driver.find_element_by_tagname (table) and so on, as well as Xpath: for example, Xpath=//*[contains (@src, ' Sysmanage/systemupgrade.action ')] and the like.
"The picture below may be a bit large, you can't see the right console, right click on the picture, new window opens"
(3) After the operation on the IFRAME, want to return to the main page to manipulate the elements, this time, you can use the Switch_to_default_content () method to return to the main page.
The following code:
iframe = Driver.find_element_by_tag_name ("iframe")
(4) How can I tell if an element is on an IFRAME?
1. After locating the element, switch to the Firepath interface.
2. Look at the upper-left corner of the Firebug tool, if the top window shows no IFRAME.
3. If the iframe#xxx is displayed, the description is on the IFRAME, #后面就是它的id.
One, the following summarizes the 5 kinds of JS localization method
Except that the ID is anchored to a single element element object, and the rest is elements returns a list object
1. Get by ID
2. Get through Name
The list is returned.
3. Selecting elements by Tag name
4. Select elements by Class
Compatibility: IE8 and the following versions of browsers do not implement the Getelementsbyclassname method
5. Selecting elements from the CSS selector
Document.queryselectorall ("CSS selector")
Compatibility: IE8 and the following versions of browsers only support CSS2 standard selector syntax
JS = ' document.getElementById ("Helloid"). Click ();
JS1 = ' Document.getelementsbyclassname ("Helloname") .value = "Taming Wang speaking"; ' The first class= "Helloname" property of the CSS style class is used throughout the HTML document, and its Value property is set to "Taming Wang speaking"
"Part Five" selenium using jquery to locate "Oh, God, the heavens are stunned, the eyes are staring round"
"See the picture, right-click the new window to open the image"
Inputtest= "$ (' #smart_input '). Val (' Handsome I can still peel ') '
Inputtest= "$ ('. Usersearch '). Val (' Handsome I can still peel ')"
Inputtest= "$ (': Text '). Val (' Handsome I can still peel ')"
Inputtest= "$ (' #searchForm > #smart_input '). Val (' Handsome I can cut it again ')
Inputtest= "$ (' #searchForm #smart_input '). Val (' Handsome I can still peel ') '" Note two ID selectors "#searchForm #smart_input" is a space in the middle "
Inputtest= "$ (' #searchForm >input:first '). Val (' Handsome I can cut it again ')
Select the last INPUT element:
Clickbutton= "$ (' #searchForm >input:last '). Click ()"
Select the number of input elements
Inputtest= "$ (' #searchForm >input:eq (0) '). Val (' Handsome I can cut again ') ' starting from 0 to count the first one
Inputtest= "$ (' #searchForm >input:nth-child (1) '). Val (' Handsome I can cut it again ')" Of course it can, nth-child. Starting from 1 counts first
1, Nth-child (n): subscript starting from 1; EQ (n): subscript starting from 0;
2, Nth-child (n): Select multiple elements; EQ (n): Select an element.
#inputTest = "$ (' input[name=query] '). Val (' Handsome I can still peel ') '
Inputtest= "$ (' input[id=smart_input] '). Val (' Handsome I can still peel ') '
"Appendix 1" Syntax usage Basics for XPath
How to use XPath:
Example 1:html/body/div/div (if you are not familiar with the HTML friends, you need to own Baidu HTML.) ）
The XPath represents the second div tag under the body tag, under the HTML tag, and the first div tag
It's good to understand.
Example 2:.//*[@id = ' content ']/div/ul
Someone will not understand.//*[@id = ' content ' What does that mean?
. Represents the current path
A//b: Descendants b tag under a tag
* Can be any label
[@id = ' content '] means ID is content
So: This example means: any sub-tags with the id content below the second div tag, below the UL label
XPath basic learning is complete, next step to start learning
P[text () = ' A ']: P tag with text a
P[text () = ' A ']: text contains P tags for a
a[@class = ' abc ']: P tag Class A (of course.) Since it is possible for @class to be able to use @id, why do not associate @src and @href it? ）
P[not (@class = ' a ')]: P tag with class not a
OK, advanced, if you want to use a higher-order XPath to first attribute the above content, then union selenium use
Comparison of XPath and other positioning methods: (mainly compared to CSS positioning)
The biggest benefit of XPath is the ability to look up, but the downside is that it's too slow.
"Appendix 2" CSS Positioning Syntax basicsCSS positioning speed is fast, many functions, but cannot find up, better than XPath, execution efficiency than XPath to come faster, have done two positioning comparison of the speed, is I think the best use of positioning method. General Usage Summary: Below are the essence OH