Thursday 7 May 2015

Define XPath for Selenium webdriver API tutorial



XPath is designed in such a way to allow the navigation of XML doc, with the purpose of selecting particular attributes, elements or some other part of an XML document for specific processing. What is an XML and its uses?
Extensible Markup Language (XML) is the context in which the XML Path Language from where XPath is exists.
XML provides a solution for standard syntax for the markup of data and documents.
XML documents can contains one or more elements. If an element contains content, whether other elements or text, then start tag and end tag must have. The text lies between the start tag and the end tag is called the elements content.
<Element> //Starting tag
Element content lies here.//Element Content or text
</Element>//Ending Tag
An element can have one or more attributes, which will gives the additional information
about the element type or their content.
Below is the sample XML format:
<?xml version='1.0'?>
<Catalog>
<Book>
<Title>XML Tutorial http://silenumnmisc.blogspot.in/</Title>
<Author>Selenium misc knowledge</Author>
</Book>
</Catalog>

It may be below like this:
<?xml version='1.0'?>
<Catalog>
<Book Title=" XML Tutorial http://silenumnmisc.blogspot.in " Author=" Selenium misc knowledge ">
</Book>
</Catalog>

XPath can be viewed as such a way to navigate round the XML documents. Thus XPath has similarities to a set of street directions.
When you need to search for an address, you should know what is your starting point(Starting points) to reach your destination place.
The starting point is called XPath, the context node.
Absolute XPath
Absolute XPath starts with the root node or a forward slash (/).
The advantage of using absolute is, it recognize the element very quickly.
main disadvantage here is, if anything goes wrong or some other tag added in between, then this path will no longer works or not in use.
ie:
If the Path we defined as below
a. html/head/body/table/tbody/tr/th
If there is a tag that has been added in between body and table as below
b. html/head/body/form/table/tbody/tr/th
The first path will no longer work as we have added 'form' tag in between
Relative Xpath
A relative xpath is one where the path starts from the node of your choice - it does not need to start from the root node.
It starts with Double (//)slash
Syntax:
//table/tbody/tr/th
Advantage of using relative xpath is, you do not need to mention the long xpath, we can start from the middle or in between.
The main disadvantage here is, its take more time in identifying the element as we specify the partial xpath path not (exact path).
If there are multiple elements for the same path, it is already select the first element that is identified from element.

No comments:

Post a Comment