Python Selenium – How can I get timedate inside of class?


This is the HTML code (LinkedIn company’s job page,

<li id="ember623" class="ember-view   jobs-search-results__list-item occludable-update p0 relative scaffold-layout__list-item

              <time datetime="2022-09-20">
                2 days ago

<!---->              </time>

I’m trying to access the date in the datetime attribute by using CLASS_NAME.

When I search using XPATH, it works, and I get the date: ‘2022-09-20’

    A = browser.find_elements(By.XPATH,"//time")

When I search for the date using class_name, like so:

    A = browser.find_elements(By.CLASS_NAME,"occludable-update")

It returns empty.

But if I write:

    A = browser.find_elements(By.CLASS_NAME,"occludable-update")

I get: ‘2 days ago’

How can I get the date, and not the text, by using By.CLASS_NAME?

Asked By: michal_levin



#if only 1 time element:
browser.find_element(By.CLASS_NAME,"time").get_attribute("datetime") #return '2022-09-20'

browser.find_element(By.CLASS_NAME,"time").text #return '2 days ago'

#if only many time element:
[i.get_attribute("datetime") for i in browser.find_elements(By.CLASS_NAME,"time").get_attribute("datetime")]
#return ['2022-09-20', ...]

[i.text for i in browser.find_elements(By.CLASS_NAME,"time").get_attribute("datetime")]
# returns ['2 days ago', ...]

if return empty, try using wait

from import WebDriverWait
from import By
from import expected_conditions as EC

WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "time"))).text
Answered By: khaled koubaa