Web Test Selenium: How to select an element

Source: Internet
Author: User
Tags xpath

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

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.