Web Test Tool Selenium: How to select an element
2009-02-17 23:23 by Agile Water, 5372 reads, 22 Comments, Favorites, compilation
Selenium is a great web testing tool and he is very good at Ajax support, and I highly recommend it.
An important part of the test is to locate the elements of the page, and selenium provides the following powerful methods for locating elements.
- ID=ID
- name=name
- dom=javascriptexpression
- XPath=xpathexpression
- link=textPattern
- css=cssselectorsyntax
I. Selection of elements through Id,name
If development is able to add an ID to a page element, then testing is a great convenience
Second, link= according to the link text to operate
Iii. selecting elements based on XPath
- xpath=//img[@alt = ' The image alt text ']
- xpath=//table[@id = ' table1 ']//tr[4]/td[2]
- Xpath=//a[contains (@href, ' #id1 ')]
- Xpath=//a[contains (@href, ' #id1 ')]/@class
- Xpath= (//table[@class = ' Stylee '))//th[text () = ' Theheadertext ']/. /td
- xpath=//input[@name = ' name2 ' and @value = ' yes ']
- Xpath=//*[text () = "Right"]
With this XPath, it's much easier for us to operate
1, through Firebug to see/html/body/form/div[5]/div/div[2]/a[5], we see this XPath is very problematic, once we change the structure, the XPath has changed
2, here I recommend a Firefox plugin XPath checker
We can use this tool to test the simplification of our XPath expressions
So if we're going to go through all the links below, we can judge the following
Determine if the first link below the navigation is "user registration"
Third, Dom selection
- dom=document.forms[' MyForm '].mydropdown
- DOM=DOCUMENT.IMAGES[56]
- Dom=function foo () {return document.links[1];}; Foo ();
Four, CSS Selector
This is not commonly used, it can support CSS2, CSS3 selector
- Css=a[href= "#id3"]
- Css=span#firstchild + span
My experience is that ID, name, XPath is basically enough, so I'm going to focus on these, and when we find the elements we need, we can use selenium's powerful features.
This paper Wang water
Without consent, no reprint