我在 Selenium 中遇到循环代码问题

问题描述 投票:0回答:1
  • 拜托,这是我的第一个项目,我还在学习Python。请耐心等待我。谢谢你们的努力

在第一次测试中,它发现元素没有任何问题。

我有一个条件,如果找到该元素“

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

”,它会让我返回主页并重新开始步骤

在重复过程中,它通知我找不到该元素

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

我希望浏览器继续并重复这些步骤,直到元素内容发生变化。

这是所有代码

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()
python selenium-webdriver while-loop seleniumbase
1个回答
0
投票

从您收到的错误消息

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

您之前的代码中似乎有这样的语句

driver.implicitly_wait(7) 

要解决此问题,您可能需要使用显式等待,即等待某些指定的条件。 显式等待的文档

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

上面的代码将在 10 秒内找到该元素时单击该元素,如果没有找到,则会抛出错误。您可能还需要检查其他条件,例如 element_to_be_clickable 而不仅仅是 presence_of_element_ located 来解决此问题。

条件满足后会跳转到下一条语句,可以是click语句。

© www.soinside.com 2019 - 2024. All rights reserved.