Selenium finding elements

Source: Internet
Author: User
Tags tagname xpath finder

1. Simple Search

by ID: Webelement element=driver.findelement (by.id ("userId"));

by Name: Webelement element=driver.findelement (by.name ("cheese"));

by TagName: Webelement element=driver.findelement (By.tagname ("a"));

by ClassName: Webelement element=driver.findelement (by.classname ("login"));

by LinkText: Webelement element=driver.findelement (By.linktext ("Blog");

This is very simple, just need to know the ID and so on the attribute value is good. So the question comes, how do you know what the attribute value is? is not always through the Web source one view it! In fact, Firefox has a very powerful plug-in, called Firebug. It is a very classic positioning elements of the Web page and view the source of the Web plugin. The installation method is to search for "Firebug" in the add-ons of Firefox, and then I choose "Firebug 2.0.8" to install.

2.Xpath

For a tutorial on XPath, refer to w3school:http://www.w3school.com.cn/xpath/

Firefox also has a plugin called the XPath Finder to make it easier to locate and to confirm that the XPath is correct. Select Add-ons and search for XPath to see that my version of this is XPath finder 1.02, click Install.

XPath uses a path expression to pick a node or set of nodes in an XML document.

Select a node

The most useful path expressions are listed below:

An expression Description
NodeName Select all child nodes of this node.
/ Select from the root node.
// Selects the nodes in the document from the current node that matches the selection, regardless of their location.
. Select the current node.
.. Selects the parent node of the current node.
@ Select the attribute.

Predicate

To find a particular node or to contain a specified node of merit. The predicate is embedded in [].

In the table below, we list some path expressions with predicates, as well as the results of expressions:

path expression result
/b OOKSTORE/BOOK[1] selects the first book element that belongs to the bookstore child element.
/bookstore/book[last ()] selects the last book element that belongs to the bookstore child element.
/bookstore/book[last ()-1] selects the penultimate book element that belongs to the bookstore child element.
/bookstore/book[position () <3] selects the first two book elements that belong to the child elements of the bookstore element.
//title[@lang] selects all the title elements that have properties named Lang.
//title[@lang = ' eng '] selects all title elements that have the Lang attribute value of Eng.
/bookstore/book[price>35.00] selects all book elements of the bookstore element, and the value of the price element must be greater than 35.0 0.
/bookstore/book[price>35.00]/title selects all the title elements of the book element in the bookstore element, where p The value of the rice element must be greater than 35.00.

Use: Webelement element=driver.findelement (By.xpath ("//ul[@class = ' nav logged_out ']/li"));

Represents the UL element that first finds the value of the class attribute "Nav logged_out", and then finds all the LI elements below these UL elements.

3.CSS Selector

In fact, most of the elements will have an ID or name, so that through the method 1 can be solved, more exotic complex can also be resolved through XPath. So why do you use CSS? Well, it's said that CSS is the fastest. This needs a bit of CSS skills. I have been watching the tutorial for a long time.

CSS Tutorial: http://www.w3school.com.cn/css/index.asp

Use:list<webelement> webelements = webdriver.findelements (By.cssselector ("Ul.nav li"));

. Nav stands for class= "NAV".

#nav represents id= "NAV".

UL represents the attribute "UL".

The above sentence means selecting all Li under ul of the class attribute value "NAV". Say, a bit around AH ~ ~

Selenium finding elements

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.