How can I find the link URL by link text with XPath?

后端 未结 6 1823
臣服心动
臣服心动 2020-12-04 08:06

I have a well formed XHTML page. I want to find the destination URL of a link when I have the text that is linked.

Example



        
相关标签:
6条回答
  • 2020-12-04 08:15

    Think of the phrase in the square brackets as a WHERE clause in SQL.

    So this query says, "select the "href" attribute (@) of an "a" tag that appears anywhere (//), but only where (the bracketed phrase) the textual contents of the "a" tag is equal to 'programming questions site'".

    0 讨论(0)
  • 2020-12-04 08:17

    if you are using html agility pack use getattributeValue:

    $doc2.DocumentNode.SelectNodes("//div[@class='className']/div[@class='InternalClass']/a[@class='InternalClass']").GetAttributeValue("href","")
    
    0 讨论(0)
  • 2020-12-04 08:19

    For case insensitive contains, use the following:

    //a[contains(translate(text(),'PROGRAMMING','programming'), 'programming')]/@href
    

    translate converts capital letters in PROGRAMMING to lower case programming.

    0 讨论(0)
  • 2020-12-04 08:20

    Should be something similar to:

    //a[text()='text_i_want_to_find']/@href
    
    0 讨论(0)
  • 2020-12-04 08:21
    //a[text()='programming quesions site']/@href 
    

    which basically identifies an anchor node <a> that has the text you want, and extracts the href attribute.

    0 讨论(0)
  • 2020-12-04 08:23

    Too late for you, but for anyone else with the same question...

    //a[contains(text(), 'programming')]/@href
    

    Of course, 'programming' can be any text fragment.

    0 讨论(0)
提交回复
热议问题