How can I extract a specific text in an html code with Selenium and Python

Rayvven
<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"))
DebanjanB

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.

edited at
0

Comments

0 comments
Login to comment

Related

How can I extract information from a HTML code using Python + Selenium?

PYTHON + SELENIUM (CHROME): How can I extract a specific text from my current url and use the extracted text to go to another?

How can I extract the text from a webelement using selenium

How to extract the text from the HTML using Selenium and Python

How can i extract href from this html using selenium?

How can I extract text between parentheses containing a specific word?

How can I look into a cell with VBA to find specific text, then extract it?

How I can extract specific target number from text file

How can I search for text in a specific part of a webpage in Selenium (Python) ? With pictures:

How can I find a DIV with a sibling with a specific text and print it Selenium Python?

How can I print an HTML text element [object Text] with Selenium?

How can we extract specific test from the text in python

how can i extract a specific element from the html

how can i extract a specific element from the html

How can I append text fetched from ajax to my html code in a specific format?

how can i extract this value from a website, with python, selenium and chromedriver

How can i extract the words which are starting with "icon" from HTML code using python

How can I add a text in textarea form (in HTML tag) by selenium python?

How can I extract the nested value from this Javascript/HTML code?

extract text from specific sections in html, python

How can I extract text fragments from PDF with their coordinates in Python?

How can I extract text from string in python?

How can I extract a text from a bytes file using python

How can i extract text from a PDF with python?

How to extract multiple text using selenium python

How to extract the text elements using Selenium in Python?

How can I both extract a specific line in a text file as well as multiple lines containing a specific string?

How can I color specific letters in html element text?

How can I extract URLs from within Javascript code? - Python

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  3. 3

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  4. 4

    pump.io port in URL

  5. 5

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  8. 8

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

  9. 9

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  10. 10

    How to remove the extra space from right in a webview?

  11. 11

    java.lang.NullPointerException: Cannot read the array length because "<local3>" is null

  12. 12

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  13. 13

    flutter: dropdown item programmatically unselect problem

  14. 14

    How to use merge windows unallocated space into Ubuntu using GParted?

  15. 15

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  16. 16

    Nuget add packages gives access denied errors

  17. 17

    Svchost high CPU from Microsoft.BingWeather app errors

  18. 18

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  19. 19

    12.04.3--- Dconf Editor won't show com>canonical>unity option

  20. 20

    Any way to remove trailing whitespace *FOR EDITED* lines in Eclipse [for Java]?

  21. 21

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

HotTag

Archive