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.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With