How to speed up the passage of a nested loop in Python - Stack Overflow

How to speed up the passage of a nested loop. I am doing browser automation using Selenium and in one s

How to speed up the passage of a nested loop. I am doing browser automation using Selenium and in one section of the code I need to compare coefficients and dates with table cells. The code needs about 30 seconds to go around the entire plate, about 6 rows and 6 columns.

    def __parse_full_page(self, url: str, data: dict = {}) -> bool:
        try:
            WebDriverWait(self.driver, 10).until(EC.visibility_of_element_located(Locator.PLAN)).click()
            cells = WebDriverWait(self.driver, 10).until(EC.visibility_of_all_elements_located(Locator.CELLS_TABLE))
            current_url = str(self.driver.current_url)
            id_ticket = current_url.split("&")[-2].split("=")[-1]
            button_planning = self.driver.find_element(*Locator.CONFIRM)

            ticket_data = tickets.get(id_ticket)

            if ticket_data != "Заявка не найдена":
                _, max_rate = ticket_data.split(':')
                max_rate = int(max_rate)
            else: max_rate = 0

            coefficients = ["Бесплатно"] + [f'✕{i}' for i in range(1, max_rate + 1)]
            
            for coefficient in coefficients:
                for cell in cells:
                    try:
                        date_text = cell.find_element(*Locator.DATE).text
                        try:
                            date_text_clean = date_text.split(',')[0].strip()
                            date_object = datetime.strptime(date_text_clean, "%d %B")
                            date_object = date_object.replace(year=datetime.now().year)
                        except ValueError as ve:
                            print(f"Ошибка при преобразовании даты: {ve}")
                            continue

                        buffer_days = int(config["BOT"]["BUFFER"])
                        today = datetime.now()
                        buffer_date = today + timedelta(days=buffer_days)

                        if date_object > buffer_date:
                            coefficient_element = cell.find_element(*Locator.RATE)
                            coefficient_text = coefficient_element.text
                            if coefficient_text.strip() == f"{coefficient}":
                                button_hover = cell.find_element(*Locator.CHOOSE_HOVER)
                                self.action.move_to_element(button_hover).perform()
                                try:
                                    cell.find_element(*Locator.CHOOSE).click()
                                    self.action.move_to_element(button_planning).click().perform()
                                    new_id_el = self.driver.find_element(*Locator.ID).text
                                    new_id = new_id_el.strip()
                                    logger.debug("ЗАЯВКА ПРОШЛА")
                                    self.__pretty_log({"id_ticket": id_ticket, 'coefficient': coefficient, 'date': date_text, "new_id": new_id})
                                    return True
                                except Exception as e:
                                    print(f"Ошибка при нажатии 'Выбрать': {e}")
                            else:
                                continue

                        else:
                            continue

                    except Exception as e:
                        print(f"Ошибка: {e}")
                        continue

        except Exception as e:
            print(f"Ошибка: {e}")

        return False


Need to speed up for coefficient in coefficients: for cell in cells: The cycle runs through the WebElement

I do not know how to speed it up.

How to speed up the passage of a nested loop. I am doing browser automation using Selenium and in one section of the code I need to compare coefficients and dates with table cells. The code needs about 30 seconds to go around the entire plate, about 6 rows and 6 columns.

    def __parse_full_page(self, url: str, data: dict = {}) -> bool:
        try:
            WebDriverWait(self.driver, 10).until(EC.visibility_of_element_located(Locator.PLAN)).click()
            cells = WebDriverWait(self.driver, 10).until(EC.visibility_of_all_elements_located(Locator.CELLS_TABLE))
            current_url = str(self.driver.current_url)
            id_ticket = current_url.split("&")[-2].split("=")[-1]
            button_planning = self.driver.find_element(*Locator.CONFIRM)

            ticket_data = tickets.get(id_ticket)

            if ticket_data != "Заявка не найдена":
                _, max_rate = ticket_data.split(':')
                max_rate = int(max_rate)
            else: max_rate = 0

            coefficients = ["Бесплатно"] + [f'✕{i}' for i in range(1, max_rate + 1)]
            
            for coefficient in coefficients:
                for cell in cells:
                    try:
                        date_text = cell.find_element(*Locator.DATE).text
                        try:
                            date_text_clean = date_text.split(',')[0].strip()
                            date_object = datetime.strptime(date_text_clean, "%d %B")
                            date_object = date_object.replace(year=datetime.now().year)
                        except ValueError as ve:
                            print(f"Ошибка при преобразовании даты: {ve}")
                            continue

                        buffer_days = int(config["BOT"]["BUFFER"])
                        today = datetime.now()
                        buffer_date = today + timedelta(days=buffer_days)

                        if date_object > buffer_date:
                            coefficient_element = cell.find_element(*Locator.RATE)
                            coefficient_text = coefficient_element.text
                            if coefficient_text.strip() == f"{coefficient}":
                                button_hover = cell.find_element(*Locator.CHOOSE_HOVER)
                                self.action.move_to_element(button_hover).perform()
                                try:
                                    cell.find_element(*Locator.CHOOSE).click()
                                    self.action.move_to_element(button_planning).click().perform()
                                    new_id_el = self.driver.find_element(*Locator.ID).text
                                    new_id = new_id_el.strip()
                                    logger.debug("ЗАЯВКА ПРОШЛА")
                                    self.__pretty_log({"id_ticket": id_ticket, 'coefficient': coefficient, 'date': date_text, "new_id": new_id})
                                    return True
                                except Exception as e:
                                    print(f"Ошибка при нажатии 'Выбрать': {e}")
                            else:
                                continue

                        else:
                            continue

                    except Exception as e:
                        print(f"Ошибка: {e}")
                        continue

        except Exception as e:
            print(f"Ошибка: {e}")

        return False


Need to speed up for coefficient in coefficients: for cell in cells: The cycle runs through the WebElement

I do not know how to speed it up.

Share Improve this question asked Nov 19, 2024 at 15:07 DourfytDourfyt 1 3
  • 1 Do you know that the slow code is inside the two for loops and not in the preamble where you call WebDriverWait()? – JonSG Commented Nov 19, 2024 at 15:20
  • @JonSG Yes, I know, I wrote about it – Dourfyt Commented Nov 19, 2024 at 15:46
  • Not being a selenium expert by any stretch, what is Locator? Micro optimization, buffer_date can probably be calculated outside the loops. – JonSG Commented Nov 19, 2024 at 15:54
Add a comment  | 

1 Answer 1

Reset to default 0

First I would remove this code from inner loop probably won't help much but doesn't appear it needs to be done there:

buffer_days = int(config["BOT"]["BUFFER"])
today = datetime.now()
buffer_date = today + timedelta(days=buffer_days)

Next is based on what you gave, without testing it myself. You are using a mouse action which tends to be slow:

self.action.move_to_element(button_planning).click().perform()

If this can be done in some other way, it should improve the performance (either click on the button directly or use JavaScript execute). If you have to have the action chain, you could try splitting it. It sometimes works faster.

self.action.move_to_element(button_planning).perform()
button_planning.click()

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742420492a4440586.html

相关推荐

  • How to speed up the passage of a nested loop in Python - Stack Overflow

    How to speed up the passage of a nested loop. I am doing browser automation using Selenium and in one s

    5小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信