Finding webelement with xpath starting with

前端 未结 3 1805
醉梦人生
醉梦人生 2021-02-02 13:54

I\'m trying to locate a link using Selenium Webdriver. I don\'t wanna locate it by link text, only the actual link which is typed in . This can be done by using Selenium\'s find

相关标签:
3条回答
  • 2021-02-02 14:26

    You can use the start-with in xpath to locate an attribute value that starts with a certain text.

    For example, assume you have the following link on the page:

    <a href="mylink_somerandomstuff">link text</a>
    

    Then you can use the following xpath to find links that have an href that starts with 'mylink':

    //a[starts-with(@href, "mylink")]
    
    0 讨论(0)
  • 2021-02-02 14:42

    You can also use contains if you don't know what the href starts with:

    <a href="link_with_key_word_in.html">Link</a>
    

    To find this link, you could use:

    By.xpath("//a[contains(@href, 'key_word')]"));
    
    0 讨论(0)
  • 2021-02-02 14:45

    Just install these addons: Firebug and FirePath in Mozilla Firefox.

    Go to the link in firefox, and invoke firebug by using F12 key.

    Then select the FirePath.

    Click the inspect element button and select the element. It will show the xpath of the element.

    Example:

    <input type = "text" id ="text-checking123"> </input>
    

    Xpath of the element will be

    //input[starts-with(@id, 'text-')]
    

    Then use it in java code.

    **Please do add comment, if the code works for you. It will help others to view the answer.**
    
    0 讨论(0)
提交回复
热议问题