Home > OS >  Python Selenium - How can I get timedate inside of class?
Python Selenium - How can I get timedate inside of class?

Time:09-23

This is the HTML code (LinkedIn company's job page, linkedin.com/yourcompany/jobs/)

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

<!---->              </time>
            </li>
          
[...]
        
    </li>

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")
    A[index].get_attribute("datetime")

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

    A = browser.find_elements(By.CLASS_NAME,"occludable-update")
    A[index].get_attribute("datetime")
    

It returns empty.

But if I write:

    A = browser.find_elements(By.CLASS_NAME,"occludable-update")
    A[index].text

I get: '2 days ago'

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

CodePudding user response:

#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 selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "time"))).text
  • Related