user1780952
user1780952

Reputation: 23

Extract 'href' from elements using Selenium

This is my current XPath:

//table//tr/td/div/div[1]/div/a/@href

It matches ten urls on a page I am looking at. It has ten matches in this form jobs/720800-Associate-Partner-Investment-Consulting-Vancouver-Job-ID-39708.aspx

I am trying to using selenium.get_text() to pull the @href string; however, my calls are pulling blanks (note: not failing, just pulling blanks). I am successfully able to pull strings on other elements on the same page.

I have searched and couldn't find any solution to my problem - does anyone have some advice?

Upvotes: 1

Views: 8355

Answers (4)

whale_steward
whale_steward

Reputation: 2248

this might be a bit late, if you are using python selenium (based on your tags) you can do it this way (as v2.44.0) :

from selenium import webdriver
# set the driver
driver = webdriver.Firefox()
# get the element
elem = driver.find_element_by_xpath('//table//tr/td/div/div[1]/div/a')
# get the attribute value
link = elem.get_attribute('href')

Upvotes: 2

Diego Pino
Diego Pino

Reputation: 11576

If I understood correctly, the problem is that for that path there are <a href="XXX"> for which href is empty and other anchors for which href is not empty. You just want to get those href which are not empty. So then, use this expression:

//table//tr/td/div/div[1]/div/a[@href!=""]/@href

Upvotes: 1

Anuragh27crony
Anuragh27crony

Reputation: 2957

just Refer to the Anchor tags and don't refer to href attributes. Once we have all elements then perform Get_Attribute() for href element....

find_elements_by_xpath("//table//tr/td/div/div[1]/div/a[@href]")
For Loop
print Each_element.Get_Attribute("href")

I hope this helps...

Upvotes: 0

Santoshsarma
Santoshsarma

Reputation: 5667

Try this

get_attribute("//table//tr/td/div/div[1]/div/a@href");

Upvotes: 0

Related Questions