Download presentation
Presentation is loading. Please wait.
1
Xpath creation
2
Agenda Xpath Overview Xpath Syntax Locating by Xpath Xpath Functions
Xpath Examples Xpath as locator in Selenium References ©2012 eTouch Systems. All rights reserved. This material contains confidential and proprietary information of eTouch Systems and may not be reproduced, distributed or disclosed, in whole or in part, without its express consent.
3
Xpath Overview XPath, the XML Path Language, is a query language for referencing particular parts of XML document. Examples that can be expressed with Xpath 1. First person element 2. Eighth child element of the second person element. 3. ID attribute of the first person element whose content is the string 'Hello' 4. All xml-stylesheet processing instructions. Locators: locators are a way to tell selenium which specific element we want it to act on. Locators can be classified into two categories: Structure-based locators: locators that rely on the structure of the page to find elements. Xpath, DOM, CSS Attributes-based locators: locators that relies on the attributes of the elements to locate them Id, Name, Link, CSS ©2012 eTouch Systems. All rights reserved. This material contains confidential and proprietary information of eTouch Systems and may not be reproduced, distributed or disclosed, in whole or in part, without its express consent.
4
Xpath Overview XPath uses path expressions to navigate in XML documents These path expressions look very much like the expressions you see when you work with a traditional computer file system The XPath language is based on a tree representation of the XML document, and provides the ability to navigate around the tree. In addition, XPath may be used to compute values (e.g., strings, numbers, or Boolean values) from the content of an XML doc. ©2012 eTouch Systems. All rights reserved. This material contains confidential and proprietary information of eTouch Systems and may not be reproduced, distributed or disclosed, in whole or in part, without its express consent.
5
XPath Syntax An XML Path Language (Xpath) expression uses a path notation, like those used in URLs, for addressing parts of an XML document. The expression is evaluated to yield an object of the node-set, Boolean, number, or string type. For example, the expression refers to the <book> elements whose type attribute is set to "Fiction". following table summarizes some of the analogous features between URLs and XPath expressions. URLs XPath expressions Hierarchy comprised of folders and files in a file system. Hierarchy comprised of elements and other nodes in an XML document. Files at each level have unique names. URLs always identify a single file. Element names at each level might not be unique. XPath expressions identify a set of all the matching elements. Evaluated relative to a particular folder, called the "current folder." Evaluated relative to a particular node called the "context" for the expression. ©2012 eTouch Systems. All rights reserved. This material contains confidential and proprietary information of eTouch Systems and may not be reproduced, distributed or disclosed, in whole or in part, without its express consent.
6
Xpath Syntax Selecting Nodes:
The node is selected by following path or steps. The most useful path expressions are listed below: ©2012 eTouch Systems. All rights reserved. This material contains confidential and proprietary information of eTouch Systems and may not be reproduced, distributed or disclosed, in whole or in part, without its express consent.
7
Xpath Syntax In the table below we have listed some path expressions and the result of the expressions: ©2011 eTouch Systems. All rights reserved. This material contains confidential and proprietary information of eTouch Systems and may not be reproduced, distributed or disclosed, in whole or in part, without its express consent.
8
Xpath Syntax Predicates:
Predicates are used to find a specific node or a node that contains a specific value. Predicates are always embedded in square brackets. In the table below we have listed some path expressions with predicates and the result of the expressions: ©2012 eTouch Systems. All rights reserved. This material contains confidential and proprietary information of eTouch Systems and may not be reproduced, distributed or disclosed, in whole or in part, without its express consent.
9
Xpath Syntax ©2012 eTouch Systems. All rights reserved. This material contains confidential and proprietary information of eTouch Systems and may not be reproduced, distributed or disclosed, in whole or in part, without its express consent.
10
Xpath Syntax Selecting Unknown Nodes
XPath wildcards can be used to select unknown XML elements. ©2011 eTouch Systems. All rights reserved. This material contains confidential and proprietary information of eTouch Systems and may not be reproduced, distributed or disclosed, in whole or in part, without its express consent.
11
Xpath Syntax Selecting Unknown Nodes
In the table below we have listed some path expressions and the result of the expressions: ©2011 eTouch Systems. All rights reserved. This material contains confidential and proprietary information of eTouch Systems and may not be reproduced, distributed or disclosed, in whole or in part, without its express consent.
12
Xpath Syntax Selecting Several Paths
By using the '| ' operator in an XPath expression you can select several paths. In the table below we have listed some path expressions and the result of the expressions: Path Expression Result //book/title | //book/price Selects all the title AND price elements of all book elements //title | //price Selects all the title AND price elements in the document /bookstore/book/title | //price Selects all the title elements of the book element of the bookstore element AND all the price elements in the document ©2011 eTouch Systems. All rights reserved. This material contains confidential and proprietary information of eTouch Systems and may not be reproduced, distributed or disclosed, in whole or in part, without its express consent.
13
Example of XML Document
<root xmlns:foo=" xmlns:bar=" <actors> <actor id="1">Christian Bale</actor> <actor id="2">Liam Neeson</actor> <actor id="3">Michael Caine</actor> </actors> <foo:singers> <foo:singer id="4">Tom Waits</foo:singer> <foo:singer id="5">B.B. King</foo:singer> <foo:singer id="6">Ray Charles</foo:singer> </foo:singers> </root> ©2011 eTouch Systems. All rights reserved. This material contains confidential and proprietary information of eTouch Systems and may not be reproduced, distributed or disclosed, in whole or in part, without its express consent.
14
Xpath Examples Xpath Examples: 1. Select the document node /
2. Select the 'root' element /root 3. Select all 'actor' elements that are direct children of the 'actors' element. /root/actors/actor 4. Select all 'singer' elements regardless of their positions in the document. //foo:singer 5. Select the 'id' attributes of the 'singer' elements regardless of their positions in the document. 6. Select the textual value of first 'actor' element. //actor[1]/text() 7. Select the last 'actor' element. //actor[last()] ©2011 eTouch Systems. All rights reserved. This material contains confidential and proprietary information of eTouch Systems and may not be reproduced, distributed or disclosed, in whole or in part, without its express consent.
15
Xpath examples 8. Select the first and second 'actor' elements using their position. //actor[position() < 3] 9. all 'actor' elements that have an 'id' attribute. 10. Select the 'actor' element with the 'id' attribute value of '3'. 11. Select all 'actor' nodes with the 'id' attribute value lower or equal to '3'. 12. Select all the children of the 'singers' node. /root/foo:singers/* 13. Select all the elements in the document. //* 14. Select all the 'actor' elements AND the 'singer' elements. //actor|//foo:singer ©2011 eTouch Systems. All rights reserved. This material contains confidential and proprietary information of eTouch Systems and may not be reproduced, distributed or disclosed, in whole or in part, without its express consent.
16
Xpath examples 15. Select the name of the first element in the document. name(//*[1]) 16. Select the numeric value of the 'id' attribute of the first 'actor' element. 17. Select the string representation value of the 'id' attribute of the first 'actor' element. 18. Select the length of the first 'actor' element's textual value. string-length(//actor[1]/text()) 19. Select the local name of the first 'singer' element, i.e. without the namespace. local-name(//foo:singer[1]) 20. Select the number of 'singer' elements. count(//foo:singer) 21. Select the sum of the 'id' attributes of the 'singer' elements. ©2011 eTouch Systems. All rights reserved. This material contains confidential and proprietary information of eTouch Systems and may not be reproduced, distributed or disclosed, in whole or in part, without its express consent.
17
XPath Functions XPath defines a certain number of functions. You can recognize a function because it has appended "()". Functions are programming constructs that will return various kinds of information's, e.g. true / false , a number , a string , a list of nodes The XPath function library is divided into four groups, each of which is described in more detail, below: ©2011 eTouch Systems. All rights reserved. This material contains confidential and proprietary information of eTouch Systems and may not be reproduced, distributed or disclosed, in whole or in part, without its express consent.
18
Xpath Functions Xpath Function Examples:
There are restrictions on how you can use functions: 1. last() last() gives the number or nodes within a context 2. position() position() returns the position of an element with respect to other children in the same parent 3. count(node-set) count gives the number of nodes in a node set 4. starts-with(string, string) returns TRUE if the second string is part of the first and starts off the first ©2011 eTouch Systems. All rights reserved. This material contains confidential and proprietary information of eTouch Systems and may not be reproduced, distributed or disclosed, in whole or in part, without its express consent.
19
Xpath Functions 6. contains(string, string)
returns TRUE if the second string is part of the first 7. string-length(string) returns the length of a string 8. number(string) transforms a string into a number 9. round(number) round a number, e.g. 1.4 becomes 1 and 1.7 becomes 2 10. translate(string1, string2) translates string1 by substituting string2 elements ©2011 eTouch Systems. All rights reserved. This material contains confidential and proprietary information of eTouch Systems and may not be reproduced, distributed or disclosed, in whole or in part, without its express consent.
20
Xpath as locator in Selenium
There are vaious strategies to locate elements in a page. You can use the most appropriate one for your case. Selenium provides the following methods to locate elements in a page: 1. find_element_by_id 2. find_element_by_name 3. find_element_by_xpath 4. find_element_by_link_text 5. find_element_by_partial_link_text 6. find_element_by_tag_name 7. find_element_by_class_name 8. find_element_by_css_selector ©2011 eTouch Systems. All rights reserved. This material contains confidential and proprietary information of eTouch Systems and may not be reproduced, distributed or disclosed, in whole or in part, without its express consent.
21
Locating by Xpath Locating by Xpath <html> <body>
<form id="loginForm"> <input name="username" type="text" /> <input name="password" type="password" /> <input name="continue" type="submit" value="Login" /> <input name="continue" type="button" value="Clear" /> </form> </body> ©2011 eTouch Systems. All rights reserved. This material contains confidential and proprietary information of eTouch Systems and may not be reproduced, distributed or disclosed, in whole or in part, without its express consent.
22
Xpath Examples The form elements can be located like this:
login_form = driver.find_element_by_xpath("/html/body/form[1]") login_form = driver.find_element_by_xpath("//form[1]") login_form = The username element can be located like this: username = username = username = The “Clear” button element can be located like this: clear_button = clear_button = ©2011 eTouch Systems. All rights reserved. This material contains confidential and proprietary information of eTouch Systems and may not be reproduced, distributed or disclosed, in whole or in part, without its express consent.
23
Questions ©2011 eTouch Systems. All rights reserved. This material contains confidential and proprietary information of eTouch Systems and may not be reproduced, distributed or disclosed, in whole or in part, without its express consent.
24
Thank you Thank you ©2011 eTouch Systems. All rights reserved. This material contains confidential and proprietary information of eTouch Systems and may not be reproduced, distributed or disclosed, in whole or in part, without its express consent.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.