Selenium using Xpath+css+javascript+jquery positioning method (treatment of selenium various localization, can not click on complications)

Source: Internet
Author: User

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)

By.xpath ("Html/body/div/form/input")

By.xpath ("//input")

2nd method: Positioning by Element index

By.xpath ("//input[4]")

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 ')]")

Description

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.

For example

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")

Driver.switch_to_frame (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.

(2) If there are multiple (level) IFRAME tags, then you have to look at the total iframe tag, to see if you are positioning the IFRAME is the array of the first number of IFRAME elements (starting from 0, based on JavaScript), The document.getElementsByTagName (' iframe ') can be entered using the Chrome browser's F12 console. Length This code, you can print out the size of the IFRAME (that is, the number), Then count starting from 0.

"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")

Driver.switch_to_frame (IFRAME)

Switch_to_default_content ()

(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.

"Part IV"selenium using JavaScript to locate

Because selenium's kernel engine is driven by JavaScript, so long as the selenium comes with the original spicy chicken method appears to be unable to locate, click the complication once the attack, you can use JavaScript big trick, in addition to JavaScript, And the great trick of jquery, one after another, you were killed, he came to the bench, the surging river never closed.

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
document.getElementById ("id")
2. Get through Name

Document.getelementsbyname ("Name")
The list is returned.
3. Selecting elements by Tag name
document.getElementsByTagName ("tag")
4. Select elements by Class
Document.getelementsbyclassname ("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

"Example Code"

JS = ' document.getElementById ("Helloid"). Click ();

Driver.execute_script (JS)

JS1 = ' Document.getelementsbyclassname ("Helloname") [0].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"

Driver.execute_script (JS1)

"Part Five" selenium using jquery to locate "Oh, God, the heavens are stunned, the eyes are staring round"

jquery is the January 2006 birth of a framework based on encapsulated JavaScript, you often see the dollar sign with a round bracket $ (' XXX '), Actually is document.getelementby what what of this JS method, as for XXX front belt. Words, is document.getelementbyclass, with the words, is document.getElementById.

"See the picture, right-click the new window to open the image"

1.Id

Inputtest= "$ (' #smart_input '). Val (' Handsome I can still peel ') '

Driver.execute_script (Inputtest)

2.Class

Inputtest= "$ ('. Usersearch '). Val (' Handsome I can still peel ')"

3. Type

Inputtest= "$ (': Text '). Val (' Handsome I can still peel ')"

4. Hierarchy

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

Knowledge Link:

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.

5. Other

#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[1]/div[2] (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[2]/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 basics

CSS 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
1. *:checked Selected * Checked element
2, Li.refined.list.group.item (if there are spaces in the middle of class, you can. +.+. Full write can also write any one)
. checkbox[type^= ' Check '][onclick*= ' Bebe '][checked$= ' Ed '] +a
(Beginning ^= contains *= end $=) +a is the A3, Li a: Select the descendants of the Li tag A5, li>a: Select the child element of the Li tag A4, li,a: Select Li and A5, Li +a: Select the next A6, Li ~a of the Li sibling: Select all A7, Li>a:last-child/li>a:last-of-type below li sibling: Select the next A A8, Li:not ([class*= ' _]): Li Calss does not contain ' _ ' Li:not (: Nth-of-type (1)): Does not contain all li9 of the first Li, Li>a:nth-child ( N)/Li>a:nth-of-type (n): Select Li under Nth a li>a:nth-last-child (n)/Li>a:nth-last-of-type (n): Select Li, Bottom n a If n = odd denotes odd number if n = even denotes an even number
Div h2:nth-child (N): When the nth child element of a div is H2 Div h2:nth-of-type (n): Div under nth h2 child element 10, Div[style]: div contains attributes style 11, li> A:only-child:li only 1 A's a label

Selenium using Xpath+css+javascript+jquery positioning method (treatment of selenium various localization, can not click on complications)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

Tags Index: