<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"))
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
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments