0
votes

I have an issue that: if 'while' started and first condition 'false' it kept going to 'else' even the 'if' became 'true', I need to stay in the loop but stop if statement to start looping all if statements, I hope I explained the question in a clear way, I tried break(), pass() and continue() and all of them stop the loop.

CurrentTime = datetime.now().time()

Actual_Time = CurrentTime.strftime("%H:%M")
Static_Time = '15:54'
while True:
    print('Teeest Looop')
    if (Actual_Time == Static_Time) :
        print('Teeest')
        options = Options()
        options.add_argument("--user-data-dir=chrome-data")
        options.add_experimental_option("excludeSwitches", ["enable-automation"])
        options.add_experimental_option('useAutomationExtension', False)
        driver = webdriver.Chrome('C:\\Users\\hana\\Downloads\\chromedriver_win32\\chromedriver.exe', options=options)
        driver.maximize_window()
        driver.get('https://web.whatsapp.com')
        time.sleep(20)
        driver.find_element_by_xpath("//*[@title='hana']").click()
        WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH,'//*[@id="main"]/footer/div[1]/div[2]/div/div[2]'))).send_keys('test sending')
        WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@class='_2Ujuu']"))).click()
        time.sleep(10)
        driver.close()       
    elif (Actual_Time != Static_Time) :
        print('Not lucky time') 
1
Are you sure of the indentation? It looks like you haven't placed an indentation inside your while loop. - Pranay Ankit
yes I'm sure but when I copied the code here it's changed - Asma Alghamdi
@Hana I don't see a for loop, or any of break, continue, pass. Where are you trying to do that? What are you trying to do, and what's happening? - blueteeth
continue works well, i guess you tried as continue() use just continue - Ali Aref
@Hana, still don't really understand what the problem is, but I can guess. You set the times outside the while loop. Then you enter the loop, unless the time is actually 15:54, you always get the else condition executing, over and over again. If you want it to stop after one iteration, either don't put it in a while loop, or add a break. If you want something different to happen, you need to set Actual_Time inside the while loop. - blueteeth

1 Answers

1
votes

You are setting values that never change outside the while loop, and then you never update them inside the loop, so the if-condition based on them never evaluates to anything different.

I think you want something like

from datetime import datetime

do_something_time = "15:54"

while True:
    current_time = datetime.now().strftime("%H:%M")
    if current_time == do_something_time:
        print("Doing something")  # this only prints at 15:54
    elif current_time == "00:00":
        break  # this exits the while loop at midnight
    else:
        print("Not this time!")