0
<a class="link__f5415c25" href="/profiles/people/1515754-andrea-jung" title="Andrea Jung">

I have above HTML element and tried using

driver.find_elements_by_class_name('link__f5415c25')

and

driver.get_attribute('href')

but it doesn't work at all. I expected to extract values in href.

How can I do that? Thanks!

2
  • Try this: document.getElementById(id).href You can also get an element by class name, but I only know this. Commented Jul 3, 2019 at 20:20
  • BTW, what is document and id? thanks! Commented Jul 3, 2019 at 20:21

1 Answer 1

1

You have to first locate the element, then retrieve the attribute href, like so:

href = driver.find_element_by_class_name('link__f5415c25').get_attribute('href')

if there are multiple links associated with that class name, you can try something like:

eList = driver.find_elements_by_class_name('link__f5415c25')
hrefList = []
for e in eList:
    hrefList.append(e.get_attribute('href'))

for href in hrefList:
    print(href)
Sign up to request clarification or add additional context in comments.

4 Comments

Cool! This actually works very well. Besides, how can I extract all herf links under this class? should I write a loop or something?
Yes, I'll update my answer, do they all have the same class name?
YES! They have the same class. Thank you.
No problem, happy programming!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.