refactor: disable calendar navigation for direct keyboard date entry and update Select2 interaction logic
This commit is contained in:
@@ -515,40 +515,40 @@ class SigaDespesasAutomation:
|
||||
await human_delay(400, 800)
|
||||
await dp.wait_for(state="visible", timeout=4000)
|
||||
|
||||
# Ajusta o mês no calendário clicando na seta esquerda (th.prev) até exibir o mês alvo (ex: JULHO)
|
||||
header_month = page.locator(".datepicker-days th.switch, .datepicker th.switch").first
|
||||
target_month_name = portuguese_months.get(target_date.month, "JULHO")
|
||||
# # Ajusta o mês no calendário clicando na seta esquerda (th.prev) até exibir o mês alvo (ex: JULHO)
|
||||
# header_month = page.locator(".datepicker-days th.switch, .datepicker th.switch").first
|
||||
# target_month_name = portuguese_months.get(target_date.month, "JULHO")
|
||||
|
||||
for attempt in range(6):
|
||||
if not await header_month.is_visible():
|
||||
break
|
||||
# for attempt in range(6):
|
||||
# if not await header_month.is_visible():
|
||||
# break
|
||||
|
||||
current_header = normalize_text(await header_month.inner_text())
|
||||
if target_month_name in current_header:
|
||||
logger.info(f"📅 Datepicker exibindo o mês correto: '{current_header}'")
|
||||
break
|
||||
# current_header = normalize_text(await header_month.inner_text())
|
||||
# if target_month_name in current_header:
|
||||
# logger.info(f"📅 Datepicker exibindo o mês correto: '{current_header}'")
|
||||
# break
|
||||
|
||||
logger.info(f"⏪ Datepicker exibe '{current_header}' (Esperado: '{target_month_name}'). Clicando na seta esquerda (th.prev) para voltar 1 mês...")
|
||||
prev_arrow = page.locator(".datepicker-days th.prev, .datepicker th.prev").first
|
||||
await mouse.click_element(prev_arrow)
|
||||
await human_delay(500, 900)
|
||||
# logger.info(f"⏪ Datepicker exibe '{current_header}' (Esperado: '{target_month_name}'). Clicando na seta esquerda (th.prev) para voltar 1 mês...")
|
||||
# prev_arrow = page.locator(".datepicker-days th.prev, .datepicker th.prev").first
|
||||
# await mouse.click_element(prev_arrow)
|
||||
# await human_delay(500, 900)
|
||||
|
||||
# Clica no dia exato das células do mês corrente (ignora .old e .new)
|
||||
cells = page.locator(".datepicker-days td.day:not(.old):not(.new)")
|
||||
count = await cells.count()
|
||||
day_str = str(target_date.day)
|
||||
# # Clica no dia exato das células do mês corrente (ignora .old e .new)
|
||||
# cells = page.locator(".datepicker-days td.day:not(.old):not(.new)")
|
||||
# count = await cells.count()
|
||||
# day_str = str(target_date.day)
|
||||
|
||||
for i in range(count):
|
||||
cell = cells.nth(i)
|
||||
if (await cell.inner_text()).strip() == day_str:
|
||||
try:
|
||||
await mouse.click_element(cell)
|
||||
logger.info(f"📅 Data selecionada no datepicker: {target_date.strftime('%d/%m/%Y')}")
|
||||
await human_delay(400, 700)
|
||||
return
|
||||
except Exception as cell_err:
|
||||
logger.warning(f"⚠️ Clique direto no dia falhou ({cell_err}). Acionando digitação direta...")
|
||||
break
|
||||
# for i in range(count):
|
||||
# cell = cells.nth(i)
|
||||
# if (await cell.inner_text()).strip() == day_str:
|
||||
# try:
|
||||
# await mouse.click_element(cell)
|
||||
# logger.info(f"📅 Data selecionada no datepicker: {target_date.strftime('%d/%m/%Y')}")
|
||||
# await human_delay(400, 700)
|
||||
# return
|
||||
# except Exception as cell_err:
|
||||
# logger.warning(f"⚠️ Clique direto no dia falhou ({cell_err}). Acionando digitação direta...")
|
||||
# break
|
||||
|
||||
# Fallback: preenchimento direto da data se o clique no calendário não fechar
|
||||
logger.warning(f"⚠️ Preenchendo a data {target_date.strftime('%d/%m/%Y')} diretamente via teclado...")
|
||||
@@ -591,7 +591,7 @@ class SigaDespesasAutomation:
|
||||
|
||||
# 2. Clica no campo de busca e preenche com fill + press ("Enter") se for AJAX ou seleciona da li
|
||||
clean_search = normalize_text(search_text)
|
||||
#await mouse.click_element(search_input)
|
||||
await mouse.click_element(search_input)
|
||||
await search_input.fill(clean_search)
|
||||
await search_input.dispatch_event("input")
|
||||
await human_delay(300, 600)
|
||||
@@ -603,6 +603,8 @@ class SigaDespesasAutomation:
|
||||
else:
|
||||
await human_delay(400, 800)
|
||||
|
||||
#Jhony todo: Adicione a verificação se realmente o item clean_search foi encontrado com caracteres permissivos.
|
||||
|
||||
# 3. Localiza e clica no item da lista (#select2-drop .select2-results li)
|
||||
results = page.locator("#select2-drop .select2-results li.select2-result-selectable")
|
||||
count = await results.count()
|
||||
@@ -616,16 +618,17 @@ class SigaDespesasAutomation:
|
||||
await mouse.click_element(item)
|
||||
logger.info(f"✅ Opção Select2 selecionada com sucesso: '{item_text.strip()}'")
|
||||
await human_delay(400, 800)
|
||||
await page.keyboard.press("Enter")
|
||||
return
|
||||
|
||||
# Fallback: se houver opção destacada ou primeira opção clicável
|
||||
logger.warning(f"⚠️ Seleção exata por texto não casou ('{target_norm}'), clicando na primeira opção visível...")
|
||||
first_opt = results.first
|
||||
if await first_opt.is_visible():
|
||||
await mouse.click_element(first_opt)
|
||||
else:
|
||||
await page.keyboard.press("Enter")
|
||||
await human_delay(500, 900)
|
||||
# # Fallback: se houver opção destacada ou primeira opção clicável
|
||||
# logger.warning(f"⚠️ Seleção exata por texto não casou ('{target_norm}'), clicando na primeira opção visível...")
|
||||
# first_opt = results.first
|
||||
# if await first_opt.is_visible():
|
||||
# await mouse.click_element(first_opt)
|
||||
# else:
|
||||
# await page.keyboard.press("Enter")
|
||||
# await human_delay(500, 900)
|
||||
|
||||
# --------------------------------------------------------------------------
|
||||
# 3. PREENCHIMENTO COMPLETO DO FORMULÁRIO DE DESPESA
|
||||
|
||||
Reference in New Issue
Block a user