Home > Enterprise >  Extract href by class Selenium Python
Extract href by class Selenium Python

Time:04-04

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  title="wheneverlilith" href="/randomusername/" tabindex="0"><span >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.

CodePudding user response:

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[@]')

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

  • Related