3

I got an issue reading XPath. Need some help/advise from experts.

Part of my HTML is below:

<div class = "input required_field">
    <div class="rounded_corner_error">
        <input id="FnameInput" class="ideField" type="text" value="" name="first_name>
           <div class ="help-tooltip">LOGIN BACK TO MAIN</div>
           <div class="error-tooltip">

I need to find the XPath of the text message (LOGIN BACK TO MAIN)

Using Firebug I find the XPath

("//html/body/div/div[5]/div/div/form/fieldset/div/div[2]/div[2]/div/div");

But using above XPath I can read only class = help-tooltip but I need to read LOGIN BACK TO MAIN.

1
  • 1
    The HTML you have posted is invalid (the name attribute of the input element is unclosed). You have also indented the last two divs as though they are children of the input, but that would be invalid HTML and is probably not representative of your structure. If you are feeding HTML (which is not valid XML) to a system that is only XML-aware, you may have problems. Commented Jun 3, 2013 at 3:38

3 Answers 3

3

Try adding /text() on the end of the xpath you have.

Sign up to request clarification or add additional context in comments.

Comments

3

It does not really look like your XPath matches your XHTML element.

You should try something simpler and more generic, such as:

//div[@class="help-tooltip"]/text()

See Selecting a css class with xpath.

Comments

2

I would use:

# Selecting the div element
//input[@id="FnameInput"]/following-sibling::div[@class="help-tooltip"]

# Selecting the text content of the div
//input[@id="FnameInput"]/following-sibling::div[@class="help-tooltip"]/text()

…since a syntactically-valid HTML document will have a unique id attribute, and as such that's a pretty strong anchor point.

Note that the latter expression will select the text node, not the text string content of that node; you need to extract the value of the text node if you want the string. How you do that depends on what tools you are using:

  • In JavaScript/DOM that would be the .nodeValue property of the text node.
  • For Nokogiri that would be the .content method.
  • …but I have no idea what technology you are using your XPath with.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.