Lara19
Lara19

Reputation: 699

Python: cannot get href from webdiver

<td>
  <div class="buy_car_list_img">
    <a href="buy_detail.aspx?TSEQNO=5002081">
      <img src="http://www.xxxxx.com//UPLOAD/AM/BF5054/26531.jpg" width="100">
    </a>
  </div>
</td>

I want to get href's url.

buy_detail.aspx?TSEQNO=5002081

Code:

g=driver.find_elements_by_class_name("buy_car_list_img")
#print(g)
for item in g:
    print(item.get_attribute('href'))

When I printed this, it returned "none", any suggestion is appreciated.

Upvotes: 1

Views: 50

Answers (2)

iamsankalp89
iamsankalp89

Reputation: 4739

You need to modified your xpath, like this. You need to pass <a> tag in xpath. Right now you are trying to point class name and try to prinr href which is not there. So need to locate anchor tag which have href and then try to print

g=driver.find_elements_by_xpath("//div[@class='buy_car_list_img']//a")
#print(g)

for item in g:
    print(item.get_attribute('href'))

Upvotes: 1

logee
logee

Reputation: 5077

The class is on the div which does not have an href attribute. You want to get the a tag. Try find_elements_by_css_selector

g = driver.find_elements_by_css_selector('.buy_car_list_img a')

Upvotes: 1

Related Questions