The XPath that was misread

Source: Internet
Author: User
Tags xpath

The XPath that was misused in the selenium

In the process of automating testing with selenium, it is a common task to select the elements on the page and perform various operations on them. Selenium offers a variety of positioning methods:

    • ID: The most effective and convenient method
    • Name: Similar to ID class
    • Class name: A good way to clean sweep some elements with the same class
    • Link text and Partial link text: Used to locate hyperlinks on more
    • Tag name: A bit similar to class name
    • CSS selector: If you try jquery, this must be the way you like it.
    • Xpath:... /HTML/BODY/DIV/DIV[2]/DIV[2]/DIV[2]/DIV[5]/DIV/P[2]

Many selenium articles on the web, when it comes to how to use XPath to locate elements, are usually said to "open the Firefox browser, install the Firebug plugin, and then easily get the XPath for that element." Because superficial understanding, for some time I really thought these look meaningless, the middle interspersed with various array operations, read anti-human anti-social and so-called XPath is the real XPath, comrades are misled.

What is xpath:http://www.w3.org/tr/xpath/
XPath Basics Tutorial: http://www.w3schools.com/xpath/default.asp

XPath has a number of drawbacks in the selenium test: 1. Poor performance, the performance of the positioning element is worse than most other methods; 2. Not strong enough, XPath will change as the layout of the page element changes; 3. Compatibility is not good, the implementation of XPath under different browsers is not the same. So many weaknesses, why does it still exist in selenium? Selenium provides these 7 element positioning tools, as if there is a hammer in the toolbox with a screwdriver, each tool can complete a specific task, provided that the correct premise, the correct use.

XPath will usually be in the following scenario: A person who writes an automated test finds that the element he wants to manipulate cannot be positioned by means of a more convenient and effective method, such as ID, name, link text, and he has not been able to persuade the person who developed the page to add the id he wants. He began to use the so-called XPath to locate the elements, the code is filled with a variety of confusing XPath (/html/body/div/div[3]/div[2]/div[4]/p[2]), in my opinion such code is no different from the recorded script. Poor readability and almost no maintenance. XPath can theoretically be used in this way, but it should be avoided in practice.

Some of the advantages of XPath are that you need to know, for example: 1. XPath can find its ancestor (ancestors) through an element, 2. Boolean logic can be judged, such as/button[@value = ' Submit ' or @name = ' Tijiao ')

Back to the scene above, if the hard-pressed person wants to navigate to a Submit button on the page, the button cannot be located by ID or name. What he's going to do at this point is not to open the Firebug position submit button, right-click the mouse and click Copy XPath. Instead, look for the developer to add the ID or name. If not, the solution can be: 1. Find the feature of the button, for example, the text of the button is submit;2. Using XPath to locate, you can write://button[@value = ' Submit '].

I am personally disgusted with using XPath, and if possible, use ID or name, as much as possible. Really want to use XPath, never open the Firebug position submit button Right click on the mouse and click "Copy XPath". First seriously study XPath, after use. In a long time, I really hate XPath, I would like to kill first and then quickly, but think of the existence is reasonable, so many Daniel did not put XPath and selenium, XPath must have its value. Recently it took some time to study XPath and read some articles about how to use XPath correctly in selenium.

Reference article:

    • Http://www.theautomatedtester.co.uk/tutorials/selenium/selenium_xpath.htm
    • Http://wiki.openqa.org/display/SEL/Help+With+XPath
    • Http://stackoverflow.com/questions/5808909/selecting-an-element-with-xpath-and-selenium
    • Https://community.neustar.biz/community/wpm/blog/2009/04/30/test-your-selenium-xpath-easily-with-firebug
    • Http://automationtricks.blogspot.com/2010/09/how-to-use-functions-in-xpath-in.html

Related Posts:

    1. Webdriver + TestNG Application
    2. SST (selenium-simple-test) Introduction
    3. Webdriver Automatic acquisition after test failure
    4. Hudson + webdriver Organization Automation test

The XPath that was misread

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.