refactor: standardize mouse interactions and enhance Select2 element handling and navigation logic

This commit is contained in:
2026-08-09 23:03:10 -04:00
parent e74eaaaf50
commit cf12715727

View File

@@ -335,7 +335,7 @@ class HumanMouse:
async def human_type(self, locator, text: str, clear_first: bool = True):
if clear_first:
await locator.click()
await self.click_element(locator)
await asyncio.sleep(random.uniform(0.1, 0.2))
await self.page.keyboard.press("Control+a")
await asyncio.sleep(random.uniform(0.05, 0.1))
@@ -511,7 +511,7 @@ class SigaDespesasAutomation:
await dp.wait_for(state="visible", timeout=4000)
except PlaywrightTimeout:
logger.warning("⚠️ Popover do Datepicker não visível no primeiro clique, tentando novamente...")
await input_locator.click()
await mouse.click_element(input_locator)
await human_delay(400, 800)
await dp.wait_for(state="visible", timeout=4000)
@@ -542,7 +542,7 @@ class SigaDespesasAutomation:
cell = cells.nth(i)
if (await cell.inner_text()).strip() == day_str:
try:
await cell.click(timeout=3000)
await mouse.click_element(cell)
logger.info(f"📅 Data selecionada no datepicker: {target_date.strftime('%d/%m/%Y')}")
await human_delay(400, 700)
return
@@ -574,25 +574,37 @@ class SigaDespesasAutomation:
mouse = self.mouse
container = page.locator(container_selector).first
await mouse.click_element(container)
try:
choice_btn = container.locator("a.select2-choice").first
if await choice_btn.is_visible(timeout=2000):
await mouse.click_element(choice_btn)
else:
await mouse.click_element(container)
except Exception:
# await mouse.click_element(container)
logger.warning("Clique nao foi possivel")
await human_delay(400, 800)
await human_delay(400, 800)
search_input = page.locator("#select2-drop .select2-input").first
search_input = page.locator("#select2-drop input.select2-input").first
await search_input.wait_for(state="visible", timeout=5000)
# Digita a busca em texto limpo sem acentos para garantir compatibilidade com Select2
# 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.human_type(search_input, clean_search, clear_first=False)
#await mouse.click_element(search_input)
await search_input.fill(clean_search)
await search_input.dispatch_event("input")
await human_delay(300, 600)
if press_enter_for_ajax:
logger.info(f"🔍 Disparando busca AJAX Select2 via ENTER para '{clean_search}'...")
await page.keyboard.press("Enter")
await human_delay(1500, 2500)
else:
await human_delay(500, 1000)
await human_delay(400, 800)
# Seleciona o item resultante normalizando ambos os lados (sem acentos, cedilhas ou caixa)
results = page.locator("#select2-drop .select2-results li")
# 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()
target_norm = normalize_text(target_option_text)
@@ -606,9 +618,13 @@ class SigaDespesasAutomation:
await human_delay(400, 800)
return
# Fallback: pressiona Enter se o primeiro item resultante for o desejado
logger.warning(f"⚠️ Seleção exata por texto não casou ('{target_norm}'), confirmando primeira opção via Enter...")
await page.keyboard.press("Enter")
# 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)
# --------------------------------------------------------------------------