feat: add persistent browser utility for session management and improve RPA element interaction logging and robustness
This commit is contained in:
48
open_browser.py
Normal file
48
open_browser.py
Normal file
@@ -0,0 +1,48 @@
|
||||
import asyncio
|
||||
import os
|
||||
from playwright.async_api import async_playwright
|
||||
|
||||
async def main():
|
||||
playwright = await async_playwright().start()
|
||||
project_root = os.path.dirname(os.path.abspath(__file__))
|
||||
user_data_dir = os.path.join(project_root, "perfil_chromium")
|
||||
|
||||
print(f"\n============================================================")
|
||||
print(f"INICIANDO CHROMIUM COM PERFIL PERSISTENTE")
|
||||
print(f"Perfil: {user_data_dir}")
|
||||
print(f"============================================================\n")
|
||||
print("-> O navegador foi aberto na pagina do SIGA.")
|
||||
print("-> Voce pode fazer login, resolver o reCAPTCHA ou salvar sessoes a vontade.")
|
||||
print("-> Para fechar, basta fechar a janela do navegador ou pressionar Ctrl+C aqui no terminal.\n")
|
||||
|
||||
context = await playwright.chromium.launch_persistent_context(
|
||||
user_data_dir=user_data_dir,
|
||||
headless=False,
|
||||
user_agent=(
|
||||
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) "
|
||||
"AppleWebKit/537.36 (KHTML, like Gecko) "
|
||||
"Chrome/124.0.0.0 Safari/537.36"
|
||||
),
|
||||
args=["--disable-blink-features=AutomationControlled"],
|
||||
ignore_default_args=["--enable-automation"],
|
||||
viewport={"width": 1366, "height": 768},
|
||||
locale="pt-BR",
|
||||
timezone_id="America/Cuiaba",
|
||||
)
|
||||
|
||||
page = context.pages[0] if context.pages else await context.new_page()
|
||||
await page.goto("https://siga.congregacao.org.br/index.aspx")
|
||||
|
||||
# Mantem o script rodando enquanto o navegador estiver aberto
|
||||
try:
|
||||
while len(context.pages) > 0:
|
||||
await asyncio.sleep(1)
|
||||
except (KeyboardInterrupt, asyncio.CancelledError):
|
||||
pass
|
||||
finally:
|
||||
await context.close()
|
||||
await playwright.stop()
|
||||
print("\n[INFO] Navegador fechado e recursos liberados.")
|
||||
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(main())
|
||||
Reference in New Issue
Block a user