2

This XPath is not working as intended, unsure as to what may be causing it to not work as intended. Here is the code snippet.

<thead>
  <tr><th>
  <a href="ContractSearchResults.m"> Contract Number</a></th>       

  driver.findElement(By.xpath("//table/thead/tr/th/a   
  [text()='Contract Number']"));
devDan
  • 5,969
  • 3
  • 21
  • 40
jdw121212
  • 23
  • 3

1 Answers1

1

Since there appears to be a space ahead of 'Contract Number', you can add it to your XPath test, or more robustly, normalize whitespace:

//table/thead/tr/th/a[normalize-space()='Contract Number']

See also Testing text() nodes vs string values in XPath

kjhughes
  • 106,133
  • 27
  • 181
  • 240
  • 1
    Thankyou so much! It was a simple answer for you but I was stumped. – jdw121212 Mar 17 '19 at 18:21
  • I noticed that you removed the text()=. if I wanted to add the space instead of the normalize, how would I do that, it is not working for me with the text()=. – jdw121212 Mar 17 '19 at 18:26
  • Generally it is best practice not to use text() in this situation. Testing the string value of the element, rather than findings its text nodes, is more robust, because the element might contain internal markup, comments, etc. In place of `normalize-space()=` you could use `.=` if you really want to treat the whitespace as significant. – Michael Kay Mar 17 '19 at 18:30
  • Yes, the [difference between testing text() nodes vs element string values](https://stackoverflow.com/q/34593753/290085) is an often-misunderstood but important distinction. Thanks, @MichaelKay. – kjhughes Mar 17 '19 at 18:49
  • //table[thead/tr/th/a/normalize-space()='Contract Number'] trying to modify response above to return the entire table that contains an to 'Contract Number' – jdw121212 Mar 17 '19 at 22:30
  • `//table[thead/tr/th/a[normalize-space()='Contract Number']]` – kjhughes Mar 17 '19 at 23:29