I have a problem with a loop code in Selenium

81 Views Asked by At
  • Please, this is my first project, I'm still learning Python. Please be patient with me. Thank you for your efforts

In the first test, it finds the elements without any issue.

I have a condition if the element is found "

def Check_Appointment(sb):
    while True:
        no_appointment_message = "We are sorry but no appointment slots are currently available. New slots open at regular intervals, please try again later"
        element_text = sb.get_text('/html/body/app-root/div/div/app-eligibility-criteria/section/form/mat-card[1]/form/div[4]')

        if no_appointment_message in element_text:
            go_to_homepage(sb)
            print("We are sorry but no appointment slots are currently available.")
            go_to_homepage(sb)
        else:
            print("Earliest available slot for Applicants")
            playsound('./Music.mp3')
            print("Attention Alarm >>>>> Success")
            get_appointment_data(sb)
            break  # Break the loop when an appointment is found

", it returns me to the home page to restart the steps anew

During the repetition it informs me that the element is not found

error encountered: Message: Element {#mat-select-value-1} was not present after 7 seconds!

I want the browser to continue and repeat the steps until the element content changes.

Here are all the codes

def click_new_booking(sb):
    sleep(5)
    # Search For Application Button
    sb.highlight_click('/html/body/app-root/div/div/app-dashboard/section[1]/div/div[2]/div/button')  
    print("Start New Booking (Button) click >>>>> Success")
    sleep(3)
    select_first_category(sb)
def select_first_category(sb):
    sleep(1)
    sb.highlight(".mt-15")
    sb.click('#mat-select-value-1') # Opens (Choose your Visa Application Centre) 'the Drop-down menu
    sb.click('span:contains("Application Centre")')
    select_second_category(sb)
def select_second_category(sb):
    sleep(1)
    sb.highlight_click('#mat-select-value-5') # Opens (Choose your appointment category) 'the Drop-down menu
    sb.click('//*[@id="mat-option-2"]/span')
    select_last_category(sb)
def select_last_category(sb):
    sleep(1)
    sb.highlight_click('#mat-select-value-3') # Opens (Choose your sub-category) 'the Drop-down menu
    sb.click('//*[@id="mat-option-3"]/span')   
    Check_Appointment(sb)    
def Check_Appointment(sb):
    while True:
        no_appointment_message = "no appointment"
        element_text = sb.get_text('/html/body/app-root/div/div/app-eligibility-criteria/section/form/mat-card[1]/form/div[4]')

        if no_appointment_message in element_text:
            go_to_homepage(sb)
        else:
            print("Earliest available slot for Applicants")
            playsound('./Music.mp3')
            print("Attention Alarm >>>>> Success")
            get_appointment_data(sb)
            break  # Break the loop when an appointment is found
def get_appointment_data(sb):
    # Search For Continue Button
    sb.highlight_click('button.mat-focus-indicator')  
    print("Continue 1 Enabled (Button) click >>>>> Success")
    sleep(4)
def go_to_homepage(sb):
    sb.highlight_click('/html/body/app-root/div/header/div[1]/div/a/img')  
    print("Back to Booking There is No Dates >>>>> Success")
    click_new_booking(sb)
with SB(uc=True, demo=True) as sb:
    while True:
        try:
            open_the_form_turnstile_page(sb)
            click_turnstile_and_verify(sb)
            def click_new_booking(sb):
        except Exception as e:
            print(f'error encountered: {e}')
        finally:        
            sleep(2)
            quit()
1

There are 1 best solutions below

0
On

From the error message that you are having

error encountered: Message: Element {#mat-select-value-1} was not present after 7 seconds

It seems that you are having a statement like this previously in the code

driver.implicitly_wait(7) 

To solve the issue you might need to use explicit wait, which would be waiting till some specified condition. Documentation of Explicit wait

element = WebDriverWait(driver, 10).until(
    EC.presence_of_element_located((By.ID, "#mat-select-value-1"))

The above code would click the element when the presence of the element is located under 10 seconds, and if it is not found then it would throw an error. You might need to check for other conditions too, like element_to_be_clickable instead of just presence_of_element_located, to solve this.

After the condition is satisfied it would move to the next statement which can be a click statement.