Using XPath to select the next-brother href attribute

I am trying to clean up the following site: http://www.hudson211.org/zf/profile/service/id/659837

I am trying to select href next to the text "web address". The following xpath switch gets the tag that I get after:

$x("//th[contains(text(), 'Web Address')]/following-sibling::td/a") 

returns

 <a href="http://www.co.sullivan.ny.us">www.co.sullivan.ny.us</a> 

However, when I specifically try to extract href using @href, the return value is an empty array:

 $x("//th[contains(text(), 'Web Address')]/following-sibling::td/a/@href") 

returns []

This is the html of the line I'm looking at:

 <tr valign="top"> <td class="profile_view_left"></td> <th align="left" class="profile_view_center">Web Address</th> <td class="profile_view_right"> <ahref="http://www.co.sullivan.ny.us">www.co.sullivan.ny.us</a> </td> <td></td> </tr> 
+6
source share
1 answer

I assume that you are using the Google Chrome console because of this $x() function. Your xpath, which selects the @href attribute, actually works , as I tested in my Chrome, only the result is not displayed in the console, as when choosing an element - for the reason that I'm not quite at the moment -:

 >var result = $x("//th[contains(text(), 'Web Address')]/following-sibling::td/a/@href") undefined >result[0].value "http://www.co.sullivan.ny.us" 

see that using the same expression, the result variable contains the expected url value. If your intention is to simply display a single href value in the console without further processing, this will do:

 >$x("//th[contains(text(), 'Web Address')]/following-sibling::td/a/@href")[0].value "http://www.co.sullivan.ny.us" 
+3
source

Source: https://habr.com/ru/post/988632/


All Articles