Home > Software engineering >  How to stop all code and go back to the beginning
How to stop all code and go back to the beginning

Time:01-11

I'm using selenium to register game account. And I use the api of the phone number rental service, but because there are too many users, some numbers will not be used. I used the if and elif methods so that when I receive the code, it will populate the web.

And I tried using start_time = time.time() to calculate if status more than 90 seconds still return 0 then stop whole code and run again from for loop, But 'return' stops only 'input_phone_number' function, not all functions. Here is the code I use:

def input_phone_number():
    while True:
        try:
            status_code = requests.get(f'https://api.viotp.com/session/getv2?requestId={request_id}&token={token}')
            code_data = status_code.json()['data']
            status = code_data['Status']
            code = code_data['Code']
            if status == 0:
                sleep(5) 
            elif status == 1:
                # Locate the element with the CSS selector '#code' and send the code as keyboard input
                driver.find_element(By.CSS_SELECTOR, '#code').send_keys(code)
                break


            if time.time() - start_time > 90:
                driver.close()
                return

And here is the original code:

for index, row in df.iterrows():
    Firstname = row['Fname']
    Lastname = row['Lname']
    Username = row['Username']
    Password = row['Password']
    
    def open_url():
        #code

    def input_information():
       #code

    def input_phone_number():
        response = requests.get(f'https://api.viotp.com/request/getv2?token={token}&serviceId=3')
        data = response.json()['data']
        number = data['phone_number']
        balance = data['balance']
        print('Phone Number:', number, '|', 'Balance:', balance)

        sleep(3)

        WebDriverWait(driver, 20).until(
            EC.presence_of_element_located((By.CSS_SELECTOR, '#phoneNumberId'))
        )
        driver.find_element(By.CSS_SELECTOR, '#phoneNumberId').send_keys(number)
        sleep(3)
        driver.find_element(By.CSS_SELECTOR, '#view_container > div > div > div.pwWryf.bxPAYd > div > div.zQJV3 > div > div.qhFLie > div > div > button > span').click()

        request_id = data['request_id']
        status = 0
        while status != 1:
            status_code = requests.get(f'https://api.viotp.com/session/getv2?requestId={request_id}&token={token}')
            code_data = status_code.json()['data']
            code = code_data['Code']
            status = code_data['Status']
            if status == 0:
                sleep(5)  # Sleep for 5 seconds before repeating the request
            elif status == 1:
                # Locate the element with the CSS selector '#code' and send the code as keyboard input
                driver.find_element(By.CSS_SELECTOR, '#code').send_keys(code)
        sleep(3)   
        driver.find_element(By.CSS_SELECTOR, '#view_container > div > div > div.pwWryf.bxPAYd > div > div.zQJV3 > div > div.qhFLie > div > div > button > span').click()

    def input_birthdate():
        #code
        
        

    open_url()
    input_information()
    input_phone_number()
    input_birthdate()

Can someone help me? Thanks very much!

CodePudding user response:

What I would do is instead of returning nothing, I would return a string "continue" and after the call to input_phone_number() I would check if the return value is "continue", and if it is I would use continue to go to the next iteration of the for loop.

def input_phone_number():
    while True:
        try:
            status_code = requests.get(f'https://api.viotp.com/session/getv2?requestId={request_id}&token={token}')
            code_data = status_code.json()['data']
            status = code_data['Status']
            code = code_data['Code']
            if status == 0:
                sleep(5) 
            elif status == 1:
                # Locate the element with the CSS selector '#code' and send the code as keyboard input
                driver.find_element(By.CSS_SELECTOR, '#code').send_keys(code)
                break


            if time.time() - start_time > 90:
                driver.close()
                return "continue"
    open_url()
    input_information()
    result = input_phone_number()
    if result == "continue":
        continue
    input_birthdate()
  • Related