Monday, May 4, 2009

How to use XPath

XPath


XPath is a small language that describes the path to parts of a web page or path of an element on the webpage. Selenium uses it to perform an action on an element on the page by idientifying them.
Like. Clicking on buttons, Filling any text field.
XPath is a very useful for selenium to idnetify an element.
Brian Slesinsky has a great tool called XPath Checker for Firefox. Its a free plugin, that is well worth installing into Firefox.
There is one of the widely used tool firebug for firefox. It allows us to inspect the elements runtime.

We can instruct selenium to identify an element on the page according to consistency and maintainabilty of our webpage.

# xpath=xpathExpression: Locate an element using an XPath expression.

* xpath=//img[@alt='The image alt text']
ex.

click
xpath=//img[@alt='Click me']


Selenium will click a link having alt value "Click me" on the page.


* xpath=//a[contains(text(),'Images')]

open
http://www.google.co.in/



assertTitle
Google



clickAndWait
xpath=//a[contains(text(),'Images')]


Selenium will click a link having text value "Click me" on the google.co.in.

* xpath=//input[@name='name2' and @value='yes']
when the element does not have unique identifcation then we can instruct selenium to identify the element with the combination of multiple attributes.

click
xpath=//button[@calss="Btn" and @type="submit"]

No comments: