[Solved] Print statement only once in a while loop (Python, Selenium) [closed]


You just need to change the order of your code, and you don’t need an if..else statement. Let it default to “Checking availability…”, and then start your loop which will refresh the page every 45 seconds until that text you specified is gone from driver.page_source

driver.get("Link of product site.")
print("Checking availability...")
while "Not available right now." in driver.page_source:
    sleep(45)
    driver.refresh()
    

print("It is available! Gonna order it now..")

If it is available immediately it will just print these both out right after each other..

Checking availability...
It is available! Gonna order it now..

Otherwise there will be a time delay between them.

If you want to add a timestamp so you know how long it took to become available use this:

import datetime

current_time = datetime.datetime.now()

driver.get("Link of product site.")
print("Checking availability...")
print(current_time)
while "Not available right now." in driver.page_source:
    sleep(45)
    driver.refresh()
    

print("It is available! Gonna order it now..")
print(current_time)

3

solved Print statement only once in a while loop (Python, Selenium) [closed]