使用 Python 和 Selenium 仅输出选定索引的文本

我在创建一个自动结帐过程的程序时遇到了一些麻烦。我正在使用 python 3 和 Selenium。该程序解析一系列日期,这些日期在页面上作为可用的四个可用插槽输出。如果当前页面上没有可用的,它将单击“下一步”按钮并搜索接下来的四个日期。如果它到达可用日期范围的末尾并且一无所获,它将等待 30 秒并重置并重新开始。


我已经完成了大部分工作,除了两个问题:


1) 我正在尝试添加一个参数,当包含该参数时,它将超越基本功能(即使用 Twilio 通过文本简单地通知用户),并完成完整的结帐过程。


这是我正在使用的python代码:


def find_available(args):


    dates_available = True

    spaces_free = False

    free_spaces = ""

    while not spaces_free:

        while dates_available:

            time.sleep(1.5)

            spots = driver.find_elements_by_css_selector('.ss-carousel-item')

            for spot_index, spot in zip(range(date_range), spots):

                if spot.value_of_css_property('display') != 'none':

                    spot.click()

                    available_dates = driver.find_elements_by_css_selector('.Date-slot-container')

                    for available_date in available_dates:

                        if available_date.value_of_css_property('display') != 'none':

                            selected_spot = available_date.find_element_by_css_selector('#slot-container-UNATTENDED')

                            if 'No doorstep delivery' not in selected_spot.text:

                                free_spaces = selected_spot.text.replace('Select a time', '').strip()

                                spaces_free = True

                            else:

                                print(selected_spot.text.replace('Select a time', '').strip())


预期行为


如果程序使用'--checkout' 或'-c' 参数启动,那么一旦'spaces-free' 设置为true,它应该发送一个带有'free_spaces' 元素内的文本的文本。然后它应该进入下一个阶段,这将是选择包含文本“Soonest available”的单选按钮(如选择包含可用时隙的第一个可用单选按钮),然后单击继续按钮.


实际行为


该程序将运行,找到一个可用的时间段,然后简单地继续到接下来的几天,从不尝试选择单选按钮并在结帐过程中前进。


我究竟做错了什么?


任何帮助,将不胜感激。


慕标5832272
浏览 145回答 1
1回答

函数式编程

在我看来,您从未在循环内设置dates_available为:Falsewhile        while dates_available:            time.sleep(1.5)            spots = driver.find_elements_by_css_selector('.ss-carousel-item')            for spot_index, spot in zip(range(date_range), spots):                if spot.value_of_css_property('display') != 'none':                    spot.click()                    available_dates = driver.find_elements_by_css_selector('.Date-slot-container')                    for available_date in available_dates:                        if available_date.value_of_css_property('display') != 'none':                            selected_spot = available_date.find_element_by_css_selector('#slot-container-UNATTENDED')                            if 'No doorstep delivery' not in selected_spot.text:                                free_spaces = selected_spot.text.replace('Select a time', '').strip()                                spaces_free = True                            else:                                print(selected_spot.text.replace('Select a time', '').strip())所以你永远不会退出while循环。如果你不想重写整个逻辑,你可以dates_available = False在你设置后立即设置spaces_free = True。这将允许退出while循环,但您可能也需要一break两个来退出for循环。如果你想要一个故障安全行为,你应该为更小的函数重构你的代码,如果你只想要第一个可用的东西,你可以只return使用第一个可用数据的函数。可能是这样的?def find_available(args):    def get_a_date():        while True:            time.sleep(1.5)            spots = driver.find_elements_by_css_selector('.ss-carousel-item')            for spot_index, spot in zip(range(date_range), spots):                if spot.value_of_css_property('display') != 'none':                    spot.click()                    available_dates = driver.find_elements_by_css_selector('.Date-slot-container')                    for available_date in available_dates:                        if available_date.value_of_css_property('display') != 'none':                            selected_spot = available_date.find_element_by_css_selector('#slot-container-UNATTENDED')                            if 'No doorstep delivery' not in selected_spot.text:                                return selected_spot.text.replace('Select a time', '').strip()                            else:                                print(selected_spot.text.replace('Select a time', '').strip())    free_spaces = get_a_date()    print('Slots Available!')    if args.checkout:        client.messages.create(to=to_mobilenumber,            from_=from_mobilenumber,            body=free_spaces)        driver.find_element_by_xpath("//*[contains(text(), 'Soonest available')]").click()        time.sleep(1.5)        driver.find_element_by_xpath("//input[@type='submit' and @value='Continue']").click()        print('Your order has been placed!')    else:        client.messages.create(to=to_mobilenumber,            from_=from_mobilenumber,            body=free_spaces)        print('Your order time will be held for the next hour.  Check your date and confirm!')
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python