Home > database >  How to loop my code every 20 seconds after it shows me the data response?
How to loop my code every 20 seconds after it shows me the data response?

Time:04-15

from ctypes.wintypes import PINT
from logging import root
from tkinter import N
from hyperlink import URL
from selenium import webdriver
import selenium
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support.expected_conditions import presence_of_element_located
import time

url = "https://blaze.com/pt/games/double"

#absolute path
firefox_driver_path = "/Users/Antônio/Desktop/roletarobo/geckodriver.exe"
firefox_options = Options()
firefox_options.add_argument("--headless")

webdriver = webdriver.Firefox(
    executable_path = firefox_driver_path,
    options = firefox_options
)

with webdriver as driver:
    # timeout
    wait = WebDriverWait(driver, 20)

    # retrieve data
    driver.get(url)

    #wait
    wait.until(presence_of_element_located((By.ID, "roulette-recent")))

    results = driver.find_elements(By.CSS_SELECTOR, "div#roulette-recent div.entry")#find_elements_by_css_selector('#roulette .sm-box .number')
    for quote in results:
      quoteArr = quote.text.split('\n')

    print([my_elem.text for my_elem in driver.find_elements(By.CSS_SELECTOR, "div#roulette-recent div.entry")][:17])
    

    

My result is below, note that whenever it shows me the result the first result is almost always [' '], it doesn't show the number, so I need it to loop until it shows me the first result, only this first one information, if in the output there is another empty information that is NOT THE FIRST, it means that it is right, because then I will put a condition on the empty results that in this case mean the number "0", unlike the first information, that if it is empty it means that it is not was loaded correctly

['', '9', '1', '4', '1', '14', '4', '3', '7', '13', '8', '12', '', '4', '3', '11', '13']

CodePudding user response:

You can loop over it like:

data = [my_elem.text for my_elem in driver.find_elements(By.CSS_SELECTOR, "div#roulette-recent div.entry")][:17]

while data[0] == '':
    # retry in 20seconds
    time.sleep(20)
    # recheck the data
    data = [my_elem.text for my_elem in driver.find_elements(By.CSS_SELECTOR, "div#roulette-recent div.entry")][:17]

I would also add a max retires condition otherwise it would loop for ever.

  • Related