Search code examples
xpathselenium-webdriverhref

using xpath to find Href in selenium webdriver


i have to find the following href using selenium in java

<tr>
   <td>
     <a target="mainFrame" href="reb.php?tiEx=ES"></a>
   </td>
</tr>

thanks


Solution

  • There are multiple ways to find the link depending on the elements it is inside and the uniqueness of the element attribute values on the page, but, from what you've provided, you can rely on the target attribute:

    //a[@target="mainFrame"]
    

    You can also narrow it down to the scope of it's parents:

    //tr/td/a[@target="mainFrame"]
    

    Also, you can additionally check the href attribute if it is persistent and reliable:

    //tr/td/a[@target="mainFrame" and @href="reb.php?tiEx=ES"]