Saranya Palanisamy
Saranya Palanisamy

Reputation: 13

how to extract the href value using selenium in java

here i want to extract the href value from below code,

<table id="offers_table" class="fixed offers breakword" summary="" width="100%" cellspacing="0">
<tbody>
<tr>
<tr>
<tr>
<td class="offer onclick ">
<table class="fixed breakword ad_id1ezENl" summary="Ad" data-photos="2" width="100%" cellspacing="0">
<tbody>
<tr>
<td rowspan="2" width="164">
<div class="space">
<span class="rel inlblk detailcloudbox">
<a class="thumb vtop inlblk rel tdnone linkWithHash scale5 detailsLink" 
href="https://www.olx.in/item/hyundai-accent-car-ID1ezENl.html#1a86c09693" title="">
</span>
</div>
</td>
<td valign="top">
<td class="wwnormal tright td-price" width="170" valign="top">
</tr>
<tr>
</tbody>
</table>

I have tried this below code but it shows the error

WebElement ele=driver.findElement(By.id("offers_table"));
  WebElement href=ele.findElement(By.xpath("//tr[3]/span[@class='rel inlblk detailcloudbox']/a[@href]"));
  System.out.println(href.getAttribute("href"));

Upvotes: 1

Views: 1452

Answers (2)

Kushal Bhalaik
Kushal Bhalaik

Reputation: 3384

You can use cssSelector insted of xpath for this case, try using below code:

 String hrefvalue = driver.findElement(By.cssSelector("span.rel.inlblk.detailcloudbox > a")).getAttribute("href");

Upvotes: 1

iamsankalp89
iamsankalp89

Reputation: 4739

Try this xpath:

//a[@class='thumb vtop inlblk rel tdnone linkWithHash scale5 detailsLink']
[@href='https://www.olx.in/item/hyundai-accent-car-ID1ezENl.html#1a86c09693']

Upvotes: 0

Related Questions