0

I have an html page with two links on it, with similar link text:

'Office of Records' and 'Brooklyn Office of Records'

If I use this xpath, it will find the second link: "//a[starts-with(.,'Brooklyn')]" - so far so good.

However, if use the same xpath but look for the first link (which starts with the word 'Office'), it doesn't work : "//a[starts-with(.,'Office')]" - 'Unable to locate element'

I tried using regex ('^Office'), but that didn't work. I tried 'contains' instead of 'starts-with', but that also didn't work since one string is a substring of the other.

Any suggestions welcome!

EDIT

I read the spec, and it is as I assumed it would work: if string1 starts with string2, it returns true: starts-with(string1, string2)

I mocked up some xml to test this in the online tester:

<?xml version="1.0"?>
    <note>
        <record>
          <a>Office</a>
          <a>Brooklyn Office</a>
        </record>
    </note>

I have found no way to point to the first node ('Office') through 'starts-with', or 'contains'

user973718
  • 225
  • 2
  • 5
  • 16
  • Take a close look at the HTML, and verify that the first link text actually starts with "Office", and doesn't have spaces or other characters at the start. – parsifal Feb 20 '13 at 19:49
  • Text has been verified. I also just tested this in an online xpath tester (http://www.xpathtester.com/test), and it returns the same results; starts-with doesn't actually seem to do what the name suggests'. – user973718 Feb 20 '13 at 20:01
  • Here's the spec: http://www.w3.org/TR/1999/REC-xpath-19991116/#function-starts-with – parsifal Feb 20 '13 at 20:09
  • Whenever I see behavior that doesn't seem to follow the spec, I try really hard to figure out what *I have done wrong.* Including writing a sample program that clearly follows the spec (ie, you construct your own document, and then apply the XPath to it). Because the likelihood that a commonly-used library only works occasionally is vanishingly small. – parsifal Feb 20 '13 at 20:14
  • Also, it's case sensitive. Make sure you're not looking for "Office" instead of "office", or apply a translate() to move everything to upper/lowercase before checking for substrings. [Look at this question](http://stackoverflow.com/questions/9804281/selectnodes-with-xpath-ignoring-cases/9805020#9805020) if you want to see how – JWiley Feb 20 '13 at 20:25

2 Answers2

0

Problem solved! So, this didn't work: //a[starts-with(.,'Office')]

This did: /*/record/a[starts-with(.,'Office')]

user973718
  • 225
  • 2
  • 5
  • 16
0

//a[text()='Office of Records'] and //a[text()='Brooklyn Office of Records']

Do not overcomplicate things ;)

Alex Okrushko
  • 7,212
  • 6
  • 44
  • 63