Search code examples
pythonselenium-webdriverselenium-chromedriverhref

How to extract href using Selenium Python?


<a class="j-search-result-value jiveTT-hover-user" data-userid="12345" href="/people/ARNLWS"><span class="j-search-result-title">Aaron Lewis</span></a>

<a class="j-search-result-value jiveTT-hover-user" data-userid="99886" href="/people/DONLDTUMP"><span class="j-search-result-title">Donald Trump</span></a>

How to extract the href from above? I tried to use find_element_by_class_name but returns NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":".j-search-result-value jiveTT-hover-user"}

My code:

driver = webdriver.Chrome(r'xxxx\chromedriver.exe')

driver.get("xxx")

eList = driver.find_element_by_class_name('j-search-result-value jiveTT-hover-user')

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

for href in hrefList:
   print(href)

The reason why I didn't use xpath because it may vary based on the URLs. Any help is appreciated! Thank you.


Solution

  • The method .find_element_by_class_name just for single class name.

    To collect elements list you have to use .find_elements not .find_element.

    I suggest you use .find_elements_by_css_selector like this:

    eList = driver.find_elements_by_css_selector('.j-search-result-value.jiveTT-hover-user')