亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用 Python 和 Selenium 僅輸出選定索引的文本

使用 Python 和 Selenium 僅輸出選定索引的文本

慕標5832272 2022-10-11 16:06:21
我在創建一個自動結帳過程的程序時遇到了一些麻煩。我正在使用 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”的單選按鈕(如選擇包含可用時隙的第一個可用單選按鈕),然后單擊繼續按鈕.實際行為該程序將運行,找到一個可用的時間段,然后簡單地繼續到接下來的幾天,從不嘗試選擇單選按鈕并在結帳過程中前進。我究竟做錯了什么?任何幫助,將不勝感激。
查看完整描述

1 回答

?
函數式編程

TA貢獻1807條經驗 獲得超9個贊

在我看來,您從未在循環內設置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!')



查看完整回答
反對 回復 2022-10-11
  • 1 回答
  • 0 關注
  • 179 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號