| |
| import asyncio |
| from playwright_stealth import stealth_async |
|
|
| class GenApeLogin: |
| def __init__(self, context, base_url, storage_state_path): |
| self.context = context |
| self.base_url = base_url |
| self.storage_state_path = storage_state_path |
|
|
| async def _clean_popups_safe(self, page): |
| for i in range(2): |
| await page.evaluate('''() => { |
| const cssSelectors = [ |
| '[data-testid="popup__gotit-btn"]', |
| '[data-testid="popup__close-btn"]', |
| '.react-joyride__tooltip button', |
| '.react-joyride__close', |
| '.css-1y0kx8f' |
| ]; |
| cssSelectors.forEach(s => { |
| const el = document.querySelector(s); |
| if (el && typeof el.click === 'function') el.click(); |
| }); |
| |
| const buttons = Array.from(document.querySelectorAll('button')); |
| const targets = ["Get Started", "Got it", "Maybe later"]; |
| buttons.forEach(btn => { |
| const txt = btn.innerText || ""; |
| if (targets.some(t => txt.includes(t))) { |
| if (typeof btn.click === 'function') btn.click(); |
| } |
| }); |
| }''') |
|
|
| if i == 0: |
| await asyncio.sleep(2) |
|
|
| async def login(self, email: str, password: str): |
| page = await self.context.new_page() |
| await stealth_async(page) |
|
|
| try: |
| await page.goto(self.base_url, wait_until="domcontentloaded", timeout=60000) |
| await asyncio.sleep(5) |
|
|
| await page.locator('.ReactModal__Content >> text="Login"').first.click() |
| await asyncio.sleep(2) |
|
|
| await page.locator('[data-testid="popup__login__use-email"]').first.click() |
| await asyncio.sleep(2) |
|
|
| await page.locator('input#email').fill(email) |
| await page.locator('input#passwd').fill(password) |
| await page.locator('[data-testid="popup__login__login-btn"]').first.click() |
|
|
| await asyncio.sleep(12) |
| await self._clean_popups_safe(page) |
|
|
| await self.context.storage_state(path=self.storage_state_path) |
| await page.screenshot(path="login_success_dashboard.png") |
|
|
| return {"ok": True, "message": "Login hoàn tất!"} |
|
|
| except Exception as e: |
| await page.screenshot(path="error_login.png") |
| return {"ok": False, "error": str(e)} |
|
|
| finally: |
| await page.close() |