Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to loop elements from webpage that keeps refreshing the web elements

I need to loop trough elements of a webpage and scrap data from each of the elements, but the webelements keep refreshing every 25 sec and my code does not finish iterating all elements in that amount of time, after that moment i get the element is not attached to the page document error:

driver.get("https://www.luckia.es/apuestas")
time.sleep(5)
driver.switch_to.frame("sbtechBC")
eventos_de_hoy=driver.find_element_by_id("today_event_btn")
eventos_de_hoy.click()
time.sleep(7)
ligi = driver.find_elements_by_class_name("leagueWindow ")
print(len(ligi))
for items in ligi:
    driver.execute_script("arguments[0].scrollIntoView(true);", items)
    nume_liga= items.find_element_by_tag_name("h5")
    print(nume_liga.text)

I am fresh all out of ideas.

like image 595
Mihai Moldovan Avatar asked Dec 13 '25 23:12

Mihai Moldovan


1 Answers

You can try below code to avoid script brake on StaleElementReferenceException:

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import StaleElementReferenceException

driver = webdriver.Chrome()
driver.get("https://www.luckia.es/apuestas")
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it("sbtechBC"))
eventos_de_hoy = driver.find_element_by_id("today_event_btn")
eventos_de_hoy.click()

ligi_len = len(WebDriverWait(driver, 10).until(EC.presence_of_all_elements_located((By.CLASS_NAME, "leagueWindow "))))
print(ligi_len)
for index in range(ligi_len):
    try:
        item = WebDriverWait(driver, 10).until(EC.presence_of_all_elements_located((By.CLASS_NAME, "leagueWindow ")))[index]
        driver.execute_script("arguments[0].scrollIntoView(true);", item)
        nume_liga = item.find_element_by_tag_name("h5")
        print(nume_liga.text)
    except StaleElementReferenceException:
        item = WebDriverWait(driver, 10).until(EC.presence_of_all_elements_located((By.CLASS_NAME, "leagueWindow ")))[index]
        driver.execute_script("arguments[0].scrollIntoView(true);", item)
        nume_liga = item.find_element_by_tag_name("h5")
        print(nume_liga.text)
like image 116
Andersson Avatar answered Dec 16 '25 20:12

Andersson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!