admin管理员组文章数量:1336660
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 Answer
Reset to default 0First 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()
本文标签: How to speed up the passage of a nested loop in PythonStack Overflow
版权声明:本文标题:How to speed up the passage of a nested loop in Python - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742420492a2471559.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
WebDriverWait()
? – JonSG Commented Nov 19, 2024 at 15:20Locator
? Micro optimization,buffer_date
can probably be calculated outside the loops. – JonSG Commented Nov 19, 2024 at 15:54