NEL89
NEL89

Reputation: 23

"While True Loop" doesn't cause the function to execute again

The title of the question is pretty much self-explanatory. I'm currently running the following code:

from selenium import webdriver
from bs4 import BeautifulSoup
import time
import datetime

url = "http://www.val-de-marne.gouv.fr/booking/create/4963/1"
cookie_banner_selector = "#cookies-banner a:nth-child(2)"
guichet_21 = "//input[@value='5955'][@name='planning']"
guichet_22 = "//input[@value='5968'][@name='planning']"
guichet_24 = "//input[@value='5973'][@name='planning']"
booking_selector = "//input[@value='Etape suivante'][@name='nextButton']"
guichet_buttons = [guichet_21, guichet_22, guichet_24]
name_guichet = ["guichet 21", "guichet 22", "guichet 24"]
guichet_buttonsandnames = zip(guichet_buttons, name_guichet)

browser = webdriver.Safari()
browser.maximize_window()
browser.get(url)
time.sleep(5)
cookie_banner = browser.find_element_by_css_selector(cookie_banner_selector)
browser.execute_script("arguments[0].click();", cookie_banner)
time.sleep(5)


def availability_checker():
    for i, j in guichet_buttonsandnames:
        guichet_selection = browser.find_element_by_xpath(i)
        guichet_selection.click()
        time.sleep(5)
        booking_submit = browser.find_element_by_xpath(booking_selector)
        booking_submit.click()
        innerHTML = browser.execute_script("return document.body.innerHTML")
        soup = BeautifulSoup(innerHTML, 'lxml')
        alert = soup.find('form', id='FormBookingCreate')
        alert_text = alert.text
        message = alert_text.strip()
        if message == "Il n'existe plus de plage horaire libre pour votre demande de rendez-vous. Veuillez recommencer ultérieurement.":
            print("No available appointments in %s" % j)
            time.sleep(5)
            browser.back()
            browser.refresh()
        else:
            print("Appointment available in %s! Hurry and complete the form!" % j)
            break


while True:
    print("Checking for appointment availabilty on %s" % datetime.datetime.now())
    availability_checker()
    print("Checking complete on %s" % datetime.datetime.now())
    time.sleep(20)
    browser.refresh()

With the intent of having the function availability_checker() restart itself every 20 seconds.

The first iteration goes smoothly as I do get a notification of whether appointments are available on the three booths I'm looking at. However, once the 20 seconds are up, all I get are the print() messages, as if the function didn't run again.

Could you please shed some light on what is happening and how can I solve it?

Thanks in advance.

Upvotes: 2

Views: 94

Answers (1)

Nic Laforge
Nic Laforge

Reputation: 1876

Using zip() will provide you with a generator and only yield the result once. You can easily fix the issue by changing:

guichet_buttonsandnames = zip(guichet_buttons, name_guichet)

To:

guichet_buttonsandnames = tuple(zip(guichet_buttons, name_guichet))

Upvotes: 1

Related Questions