簡體   English   中英

Python selenium 腳本在添加任何延遲時崩潰

[英]Python selenium script crashes when any delay is added

我需要在那里添加延遲,但任何類型的延遲都會使代碼崩潰。 我需要它,因為這個腳本點擊得如此之快,以至於 web 不能同時處理這兩個,只能注冊第二個。

        """ Returns a pair of web elements corresponding to the first of the morning
        and afternoon bunch of slots*'"""
            
        available_slot_pairs = []
        """ The list of available to book slots """

        available_rows = []

        rows = self.driver.find_elements_by_class_name("fc-event-container")

        for row_index in range(row_offset, len(rows)):
            """ Range is determined by row_offset & number of rows(18)"""

            row = (rows[row_index])

            self.boxcheck(By.CLASS_NAME, "fc-timeline-event")

            slots = row.find_elements_by_class_name("fc-timeline-event")

            slot = slots[0]

            slot6 = slots[6]

            if 's-lc-eq-avail' in slot.get_attribute("class") and 's-lc-eq-avail' in slot6.get_attribute("class"):

                available_slot_pairs.append((slot, slot6))
                    
                available_rows.append(row_index)

        return available_slot_pairs, available_rows                 
    
    def select_seats(self,seat_num):
        """ Clicks the desired available seats and looks for the "SUBMIT TIMES" button to register the selected seats*'"""

        available_slot_pairs, available_rows = self.find_first_available_slots_elements(row_offset=0)
            
        seats_selected = 0     
                            
        for morning_slots_first, afternoon_slots_first in available_slot_pairs:

            morning_slots_first.click()

            #I NEED TO ADD DELAY HERE, BUT NOTHING SEEMS TO WORK <-----------------------------------

            afternoon_slots_first.click()

            seats_selected+=1

            if seats_selected >= seat_num:

                self.fasten(By.ID, 'submit_times')

                self.driver.find_element_by_id('submit_times').click()

                return                 

我找不到任何解決方案。 我知道在第一次 click() 之后,如果我添加這個:self.fasten(By.ID, 'submit_times'),腳本將等待 web 處理點擊,有什么想法嗎?

錯誤: Message: stale element reference: element is not attached to the page document

如果不添加延遲,則不會出現此錯誤

使用睡眠或使用此代碼讓網站的特定部分加載

try:
    myElem = WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.ID, 'ID')))
    print("Page is ready!")
except TimeoutException:
    print("Loading took too much time!")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM