Search code examples
pythonseleniumselenium-webdriverhiddenwebdriverwait

How to extract a list from html with selenium and then use it in python script to find elements


Im trying to extract a list from webpage using selenium and python. I need to import it as list in order to find elements inside that list later using python code.

this is how its stored in the webpage:

...<input type="hidden" name="GridContainerDataV" value="[["7131090","Arvejas, Enteras Verdes","400","",""],["71311099","Arvejas, Enteras Azules","520","abril/2021","mayo/2021"],["71311100","lo que sea","720","junio/2021","diciembre/2021"],...]]" autocomplete="off">

This is how I tried to extracted (Im super begginer in python):

try:
    tabla = WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.NAME, "GridContainerDataV")))
    print(tabla.value)

Solution

  • The list:

    [["7131090","Arvejas, Enteras Verdes","400","",""],["71311099","Arvejas, Enteras Azules","520","abril/2021","mayo/2021"],["71311100","lo que sea","720","junio/2021","diciembre/2021"],...]]
    

    is the value of the value attribute of the <input> tag and is hidden. To extract the list you need to induce WebDriverWait for the presence_of_element_located() and you can use either of the following Locator Strategies:

    • Using NAME:

      print(WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.NAME, "GridContainerDataV"))).get_attribute("value"))
      
    • Using CSS_SELECTOR:

      print(WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.CSS_SELECTOR, "input[name='GridContainerDataV']"))).get_attribute("value"))
      
    • Using XPATH:

      print(WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.XPATH, "//input[@name='GridContainerDataV']"))).get_attribute("value"))
      
    • 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