1
<time class="_1o9PC Nzb55" datetime="2020-06-07T17:45:25.000Z" title="7. Juni 2020">Vor 1 Stunde</time>

I am currently web scraping with selenium. The code you see is the html element of when a picture got posted on instagram. I want the code to just print this:

datetime="2020-06-07T17:45:25.000Z"

Say I found the element by class and do print(element.text). Then it outputs this: "Vor 1 Stunde" (sorry for being in german). I don't know if there even is a way to do this but if there is, please let me know. This is the whole code:

from selenium import webdriver
import time, pyautogui, random
browser = webdriver.Firefox()
browser.get('https://www.instagram.com/')
time.sleep(1)
name = browser.find_element_by_xpath("/html/body/div[1]/section/main/article/div[2]/div[1]/div/form/div[2]/div/label/input")
name.click()
name.send_keys("username")
passwort = browser.find_element_by_xpath("/html/body/div[1]/section/main/article/div[2]/div[1]/div/form/div[3]/div/label/input")
passwort.send_keys("password")

browser.find_element_by_xpath("/html/body/div[1]/section/main/article/div[2]/div[1]/div/form/div[4]/button/div").click()
time.sleep(3)
browser.find_element_by_xpath("/html/body/div[1]/section/main/div/div/div/div/button").click()
time.sleep(2)
browser.find_element_by_xpath("/html/body/div[4]/div/div/div[3]/button[2]").click()

time.sleep(2)

suche = browser.find_element_by_class_name("LWmhU").click()
time.sleep(1)
pyautogui.typewrite("mmd")
pyautogui.typewrite(["enter"])
time.sleep(2.5)
acc = browser.find_element_by_xpath("/html/body/div[1]/section/nav/div[2]/div/div/div[2]/div[2]/div[2]/div/a[1]/div/div[2]/span").click()
print(acc)
time.sleep(1)
# click on the instagram picture
pyautogui.click(427, 754)
time.sleep(2)
uploaddate = browser.find_element_by_class_name("_1o9PC")
print(uploaddate.getAttribute("datetime"))
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Rayvven
  • 13
  • 5

1 Answers1

0

The desired element is a ReactJS enabled element so to locate the element you need to induce WebDriverWait for the visibility_of_element_located() and you can use either of the following Locator Strategies:

  • Using XPATH:

    print(WebDriverWait(browser, 20).until(EC.visibility_of_element_located((By.XPATH, "//time[text()='Vor 1 Stunde']"))).get_attribute("datetime"))
    
  • Using CSS_SELECTOR:

    print(WebDriverWait(browser, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "time[title$='Juni 2020'][datetime]"))).get_attribute("datetime"))
    
  • Note : You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • sure! one more question: i can use the webdriver wait instead of the time.sleep in order to wait till the page is loaded right? but when i do so i always get a syntax error on the next line. – Rayvven Jun 07 '20 at 21:28
  • @Rayvven You should never use the raw `time.sleep()` to wait. Always use _WebdriverWait_ – undetected Selenium Jun 07 '20 at 21:30
  • okay, i tried that but it didnt work. i got a syntax error for the line after the WebdriverWait line, which didnt make any sense (for me). Is that common or do you know what it could be? – Rayvven Jun 07 '20 at 21:49
  • @Rayvven Can you raise a new question with the new requirement please so that we can have a fresh look at your current problem? – undetected Selenium Jun 07 '20 at 21:50
  • yeah okay, but i'll do that later when i have more time – Rayvven Jun 07 '20 at 22:01