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.

Phrogz
  • 296,393
  • 112
  • 651
  • 745
Raju
  • 31
  • 1
  • 3
  • 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. – Phrogz Jun 03 '13 at 03:38

3 Answers3

3

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

Matthew Strawbridge
  • 19,940
  • 10
  • 72
  • 93
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.

Community
  • 1
  • 1
Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
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.
Phrogz
  • 296,393
  • 112
  • 651
  • 745