简体   繁体   中英

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)

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

you should use the get_attribute function

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM