0

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') 
12
  • Are you sure of the indentation? It looks like you haven't placed an indentation inside your while loop. Commented Feb 20, 2021 at 12:33
  • yes I'm sure but when I copied the code here it's changed Commented Feb 20, 2021 at 12:34
  • @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? Commented Feb 20, 2021 at 12:37
  • 2
    continue works well, i guess you tried as continue() use just continue Commented Feb 20, 2021 at 12:50
  • 1
    @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. Commented Feb 20, 2021 at 13:01

1 Answer 1

1

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!")
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.