Monday, December 21, 2015

Xpath

Note:
- xpath("") --- location paths should be in double quotes
- xpath("//node[@attribute='xcvv']") --- every attribute declaration should be inside [] and value of attribute should be in single quote.

Regular Expression in Xpath??


  • We can join two conditions using and
          xpath = //<tag name>[@<attribute name>='<value>'  and @value='1']

  • Use Higlight, adjacent to Xpath section in inspect by Firepath window, when you click Highlight it will show the web element that xpath is refering too. That way you can validate the xpath, whether its pointing out to the proper web element. Dont forget to enter xpath in the field.
  • Regular Expression in xpath:
           xpath = //<tag name>[starts-with(@<attribute name>,'<value>')]

           Following could be used:
           ends-with
           contains


12/21/2015

Source: http://www.w3.org/TR/xpath/

Here are some examples of location paths using abbreviated syntax:
  • para selects the para element children of the context node
  • * selects all element children of the context node
  • text() selects all text node children of the context node
  • @name selects the name attribute of the context node
  • @* selects all the attributes of the context node
  • para[1] selects the first para child of the context node
  • para[last()] selects the last para child of the context node
  • */para selects all para grandchildren of the context node
  • /doc/chapter[5]/section[2] selects the second section of the fifth chapter of the doc
  • chapter//para selects the para element descendants of the chapter element children of the context node
  • //para selects all the para descendants of the document root and thus selects all para elements in the same document as the context node
  • //olist/item selects all the item elements in the same document as the context node that have an olist parent
  • . selects the context node
  • .//para selects the para element descendants of the context node
  • .. selects the parent of the context node
  • ../@lang selects the lang attribute of the parent of the context node
  • para[@type="warning"] selects all para children of the context node that have a type attribute with value warning
  • para[@type="warning"][5] selects the fifth para child of the context node that has a type attribute with value warning
  • para[5][@type="warning"] selects the fifth para child of the context node if that child has a type attribute with value warning
  • chapter[title="Introduction"] selects the chapter children of the context node that have one or more title children with string-value equal to Introduction
  • chapter[title] selects the chapter children of the context node that have one or more title children
  • employee[@secretary and @assistant] selects all the employee children of the context node that have both a secretary attribute and an assistant attribute
The most important abbreviation is that child:: can be omitted from a location step. In effect, child is the default axis. For example, a location path div/para is short for child::div/child::para.
There is also an abbreviation for attributes: attribute:: can be abbreviated to @. For example, a location path para[@type="warning"] is short for child::para[attribute::type="warning"] and so selects para children with a type attribute with value equal towarning.
// is short for /descendant-or-self::node()/. For example, //para is short for /descendant-or-self::node()/child::para and so will select any para element in the document (even a para element that is a document element will be selected by //para since the document element node is a child of the root node); div//para is short for div/descendant-or-self::node()/child::para and so will select all para descendants of div children.
NOTE: The location path //para[1] does not mean the same as the location path /descendant::para[1]. The latter selects the first descendant para element; the former selects all descendant para elements that are the first para children of their parents.
A location step of . is short for self::node(). This is particularly useful in conjunction with //. For example, the location path .//para is short for
self::node()/descendant-or-self::node()/child::para
and so will select all para descendant elements of the context node.
Similarly, a location step of .. is short for parent::node(). For example, ../title is short for parent::node()/child::title and so will select the title children of the parent of the context node.


More examples are covered in the following website

http://hedleyproctor.com/2011/05/tutorial-writing-xpath-selectors-for-selenium-tests/
There are a number of ways to write and test an XPath expression:
  1. Using the Selenium IDE itself – it has a “test” option.
  2. Using Firebug and a separate XPath tester.
  3. Using Firebug and the FirePath tester, which is a separate add-on but integrates with Firebug.

Other websites:

http://zvon.org/comp/r/ref-XPath_1.html


3/2/2016:

Ways of Validating Xpath:

xpath could be validated or written on own to find web objects in web pages. And process of writing and finding xpath is possible in many ways, for an example in

My Experience:

Chrome 
- One could write the xpath by following below process

F12->ctrl+F-> Enter xpath in the field opened.

- To generate xpath, you could go to element, right click on it -> Inspect -> Go to the inspect code and Right click -> Copy the xpath.

You could use this process, when ever you are working on finding element in a web page.

FireFox:

Firebug and Firepath  are the two addins that would help you to validate your xpath.

Source:
http://stackoverflow.com/questions/22571267/how-to-verify-an-xpath-expression-in-chrome-developers-tool-or-firefoxs-firebug


1. Chrome

This can be achieved by three different approaches (see my blog article here for more details):
  • Search in Elements panel
  • Execute $x() and $$() in Console panel, as shown in Lawrence's answer
  • Third party extensions (not necessary)
Here is how you search XPath in Elements panel:
  1. Press F12 to open Chrome Developer Tool
  2. In "Elements" panel, press Ctrl+F
  3. In the search box, type in XPath or CSS Selector, if elements are found, they will be highlighted in yellow.
enter image description here

2. Firefox

  1. Install Firebug
  2. Install Firepath
  3. Press F12 to open Firebug
  4. Switch to FirePath panel
  5. In dropdown, select XPathor CSS
  6. Type in to locate
enter image description here

3. Chrome and Firefox

You can open a Console in Chrome, and check the XPath by typing $x("your_xpath_here"). This will return an array of matched values. If it is empty, you know there is no match on the page.

enter image description here

53 comments:

  1. How about validating your xpath in Internet Explorer??

    ReplyDelete
  2. What is wrong with below xpath:
    $x("//span[@class='title'][1]") ---it supposed to locate first 'span' tag.

    ReplyDelete
  3. The knowledge of technology you have been sharing thorough this post is very much helpful to develop new idea. here by i also want to share this.
    Digital Marketing online training

    full stack developer training in pune

    full stack developer training in annanagar

    ReplyDelete
  4. This is a nice article here with some useful tips for those who are not used-to comment that frequently. Thanks for this helpful information I agree with all points you have given to us. I will follow all of them.
    python training institute in chennai
    python training in Bangalore
    python training in pune
    python training institute in chennai
    python training in velachery
    python online training

    ReplyDelete
  5. I wanted to thank you for this great read!! I definitely enjoying every little bit of it I have you bookmarked to check out new stuff you post.is article.
    Blueprism training in Pune

    Blueprism online training

    Blue Prism Training in Pune

    ReplyDelete
  6. Hello I am so delighted I found your blog, I really found you by mistake, while I was looking on Yahoo for something else, anyways I am here now and would just like to say thanks for a tremendous post. Please do keep up the great work.
    Data Science training in kalyan nagar | Data Science training in OMR
    selenium training in chennai | Data Science training in chennai
    Data science training in velachery | Data science online training

    ReplyDelete
  7. I am a regular reader of your blog and being students it is great to read that your responsibilities have not prevented you from continuing your study and other activities. Love
    java training in chennai | java training in bangalore

    java online training | java training in pune

    ReplyDelete
  8. I feel happy to find your post, excellent way of writing and also I would like to share with my colleagues so that they also get the opportunity to read such an informative blog.
    Selenium Training in Chennai
    Selenium Training
    Selenium Course in Chennai
    .Net coaching centre in chennai
    PHP Training Institute in Chennai
    J2EE Training in Chennai
    core Java training in chennai

    ReplyDelete
  9. Hi,
    I must appreciate you for providing such a valuable content for us. This is one amazing piece of article. Helped a lot in increasing my knowledge.
    DevOps course in Chennai
    DevOps course
    Best devOps Training in Chennai
    DevOps foundation certificate
    DevOps institute certification
    DevOps certification course

    ReplyDelete
  10. Nice post. I learned some new information. Thanks for sharing.

    Article submission sites

    Education

    ReplyDelete
  11. This post is much helpful for us. This is really very massive value to all the readers and it will be the only reason for the post to get popular with great authority.
    Ethical Hacking Course in Chennai 
    SEO Training in Chennai
    Hacking Course in Chennai 
    Ethical Hacking Training in Chennai 
    SEO Training Institute in Chennai
    SEO training course

    ReplyDelete
  12. your blog information's are really creative and It contains full of new innovative ideas.thank you for sharing with us.please update more data.
    Angularjs Training Bangalore
    Angularjs courses in Bangalore
    Angular JS Training courses near me
    AngularJS Training in Amjikarai

    ReplyDelete
  13. Really very happy to say that your post is very interesting. I never stop myself to say something about it. You did a great job. Keep it up.
    We have an excellent IT courses training institute in Hyderabad. We are offering a number of courses that are very trendy in the IT industry. For further information, please once go through our site.CEH Training In Hyderabad

    ReplyDelete
  14. Wow its a very good post. The information provided by you is really very good and helpful for me. Keep sharing good information.
    BEST ANGULAR JS TRAINING IN CHENNAI WITH PLACEMENT

    https://www.acte.in/angular-js-training-in-chennai
    https://www.acte.in/angular-js-training-in-annanagar
    https://www.acte.in/angular-js-training-in-omr
    https://www.acte.in/angular-js-training-in-porur
    https://www.acte.in/angular-js-training-in-tambaram
    https://www.acte.in/angular-js-training-in-velachery

    ReplyDelete
  15. This post is so interactive and informative.keep update more information...
    DevOps course in Tambaram
    DevOps Training in Chennai

    ReplyDelete
  16. This post is so interactive and informative.keep updating more information...
    Future Of Java Programming
    Java Career

    ReplyDelete