Skip to content
Advertisement

Extract href by class Selenium Python

Im trying to get all hrefs that are found the a specific class next to them. Im not really familiar with HTML so I’m having some trouble.

Basically the HTML code in inspect is:

<a class="notranslate _0imsa " title="wheneverlilith" href="/randomusername/" tabindex="0"><span class="_7UhW9   xLCgt        qyrsm KV-D4           se6yk       T0kll ">randomusername</span></a>

I’m using the find_elements command to find all lines that include the class “notranslate _0imsa ” like this:

links = driver.find_elements_by_class_name('notranslate _0imsa ')

I then extract all hrefs from “links” with the following command:

for link in links:
    user = link.get_attribute('href')
    users.add(user)

When I try to print the lengths of “users” and “links” they all show 0.

Advertisement

Answer

Compound class names (one separated with spaces: notranslate _0imsa) are not permitted.

Selenium wouldn’t be able to find those elements this way.

Try this instead:

links = driver.find_elements(By.XPATH, '//a[@class="notranslate _0imsa "]')

NOTE: you have to import By:

from selenium.webdriver.common.by import By

find_elements_by_class_name, find_element_by_xpath etc. are deprecated

Read more about compound class names in this article: Selenium compound class names not permitted

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement