Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1315,21 +1315,22 @@ async def click_element(request: dict, token: str = Depends(verify_token)):
|
|
| 1315 |
except Exception:
|
| 1316 |
pass
|
| 1317 |
|
| 1318 |
-
# Obter tamanho real da viewport
|
| 1319 |
viewport_width = driver.execute_script("return window.innerWidth;")
|
| 1320 |
viewport_height = driver.execute_script("return window.innerHeight;")
|
| 1321 |
|
| 1322 |
-
# Escalar coordenadas
|
| 1323 |
actual_x = int(x * viewport_width / img_width) if img_width > 0 else int(x)
|
| 1324 |
actual_y = int(y * viewport_height / img_height) if img_height > 0 else int(y)
|
| 1325 |
-
|
| 1326 |
-
# Garantir limites
|
| 1327 |
actual_x = max(0, min(actual_x, viewport_width - 1))
|
| 1328 |
actual_y = max(0, min(actual_y, viewport_height - 1))
|
| 1329 |
|
| 1330 |
logger.info(f"Click: preview({x},{y}) img({img_width}x{img_height}) -> real({actual_x},{actual_y}) viewport({viewport_width}x{viewport_height})")
|
| 1331 |
|
| 1332 |
-
#
|
|
|
|
|
|
|
|
|
|
| 1333 |
element_info = driver.execute_script(f"""
|
| 1334 |
var elem = document.elementFromPoint({actual_x}, {actual_y});
|
| 1335 |
if (elem) {{
|
|
@@ -1345,7 +1346,7 @@ async def click_element(request: dict, token: str = Depends(verify_token)):
|
|
| 1345 |
return null;
|
| 1346 |
""")
|
| 1347 |
|
| 1348 |
-
# Clicar via JavaScript
|
| 1349 |
driver.execute_script(f"""
|
| 1350 |
var elem = document.elementFromPoint({actual_x}, {actual_y});
|
| 1351 |
if (elem) {{
|
|
@@ -1353,33 +1354,53 @@ async def click_element(request: dict, token: str = Depends(verify_token)):
|
|
| 1353 |
elem.click();
|
| 1354 |
}} else {{
|
| 1355 |
var evt = new MouseEvent('click', {{
|
| 1356 |
-
bubbles: true,
|
| 1357 |
-
|
| 1358 |
-
clientX: {actual_x},
|
| 1359 |
-
clientY: {actual_y},
|
| 1360 |
-
view: window
|
| 1361 |
}});
|
| 1362 |
-
document.
|
| 1363 |
}}
|
| 1364 |
""")
|
| 1365 |
|
|
|
|
| 1366 |
import time
|
| 1367 |
-
|
| 1368 |
-
|
| 1369 |
-
|
| 1370 |
-
|
| 1371 |
-
|
| 1372 |
-
|
| 1373 |
-
|
| 1374 |
-
|
| 1375 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1376 |
|
| 1377 |
# Screenshot
|
| 1378 |
screenshot = None
|
| 1379 |
try:
|
| 1380 |
screenshot = driver.get_screenshot_as_base64()
|
| 1381 |
-
except Exception:
|
| 1382 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1383 |
|
| 1384 |
current_url = ""
|
| 1385 |
title = ""
|
|
@@ -1392,6 +1413,8 @@ async def click_element(request: dict, token: str = Depends(verify_token)):
|
|
| 1392 |
session["last_url"] = current_url
|
| 1393 |
session["last_activity"] = time.time()
|
| 1394 |
|
|
|
|
|
|
|
| 1395 |
return {
|
| 1396 |
"success": True,
|
| 1397 |
"message": f"Clique em ({actual_x}, {actual_y})",
|
|
@@ -1400,14 +1423,27 @@ async def click_element(request: dict, token: str = Depends(verify_token)):
|
|
| 1400 |
"url": current_url,
|
| 1401 |
"current_url": current_url,
|
| 1402 |
"title": title,
|
| 1403 |
-
"screenshot": screenshot
|
|
|
|
| 1404 |
}
|
| 1405 |
|
| 1406 |
except HTTPException:
|
| 1407 |
raise
|
| 1408 |
except Exception as e:
|
| 1409 |
logger.error(f"Erro no clique: {e}")
|
| 1410 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1411 |
# ============================================================
|
| 1412 |
# SEARCH SITE
|
| 1413 |
# ============================================================
|
|
|
|
| 1315 |
except Exception:
|
| 1316 |
pass
|
| 1317 |
|
| 1318 |
+
# Obter tamanho real da viewport
|
| 1319 |
viewport_width = driver.execute_script("return window.innerWidth;")
|
| 1320 |
viewport_height = driver.execute_script("return window.innerHeight;")
|
| 1321 |
|
| 1322 |
+
# Escalar coordenadas
|
| 1323 |
actual_x = int(x * viewport_width / img_width) if img_width > 0 else int(x)
|
| 1324 |
actual_y = int(y * viewport_height / img_height) if img_height > 0 else int(y)
|
|
|
|
|
|
|
| 1325 |
actual_x = max(0, min(actual_x, viewport_width - 1))
|
| 1326 |
actual_y = max(0, min(actual_y, viewport_height - 1))
|
| 1327 |
|
| 1328 |
logger.info(f"Click: preview({x},{y}) img({img_width}x{img_height}) -> real({actual_x},{actual_y}) viewport({viewport_width}x{viewport_height})")
|
| 1329 |
|
| 1330 |
+
# Guardar URL antes do clique
|
| 1331 |
+
original_url = driver.current_url
|
| 1332 |
+
|
| 1333 |
+
# Detectar elemento
|
| 1334 |
element_info = driver.execute_script(f"""
|
| 1335 |
var elem = document.elementFromPoint({actual_x}, {actual_y});
|
| 1336 |
if (elem) {{
|
|
|
|
| 1346 |
return null;
|
| 1347 |
""")
|
| 1348 |
|
| 1349 |
+
# Clicar via JavaScript
|
| 1350 |
driver.execute_script(f"""
|
| 1351 |
var elem = document.elementFromPoint({actual_x}, {actual_y});
|
| 1352 |
if (elem) {{
|
|
|
|
| 1354 |
elem.click();
|
| 1355 |
}} else {{
|
| 1356 |
var evt = new MouseEvent('click', {{
|
| 1357 |
+
bubbles: true, cancelable: true,
|
| 1358 |
+
clientX: {actual_x}, clientY: {actual_y}, view: window
|
|
|
|
|
|
|
|
|
|
| 1359 |
}});
|
| 1360 |
+
document.body.dispatchEvent(evt);
|
| 1361 |
}}
|
| 1362 |
""")
|
| 1363 |
|
| 1364 |
+
# Esperar navegacao com timeout curto
|
| 1365 |
import time
|
| 1366 |
+
max_wait = 8
|
| 1367 |
+
waited = 0
|
| 1368 |
+
while waited < max_wait:
|
| 1369 |
+
time.sleep(0.5)
|
| 1370 |
+
waited += 0.5
|
| 1371 |
+
try:
|
| 1372 |
+
# Verificar se mudou de janela
|
| 1373 |
+
handles = driver.window_handles
|
| 1374 |
+
if handles:
|
| 1375 |
+
driver.switch_to.window(handles[-1])
|
| 1376 |
+
# Verificar se pagina carregou
|
| 1377 |
+
ready = driver.execute_script("return document.readyState;")
|
| 1378 |
+
if ready == "complete":
|
| 1379 |
+
break
|
| 1380 |
+
except Exception:
|
| 1381 |
+
time.sleep(0.5)
|
| 1382 |
+
waited += 0.5
|
| 1383 |
+
try:
|
| 1384 |
+
handles = driver.window_handles
|
| 1385 |
+
if handles:
|
| 1386 |
+
driver.switch_to.window(handles[-1])
|
| 1387 |
+
except Exception:
|
| 1388 |
+
pass
|
| 1389 |
+
break
|
| 1390 |
|
| 1391 |
# Screenshot
|
| 1392 |
screenshot = None
|
| 1393 |
try:
|
| 1394 |
screenshot = driver.get_screenshot_as_base64()
|
| 1395 |
+
except Exception as e:
|
| 1396 |
+
logger.warning(f"Screenshot falhou apos clique: {e}")
|
| 1397 |
+
try:
|
| 1398 |
+
handles = driver.window_handles
|
| 1399 |
+
if handles:
|
| 1400 |
+
driver.switch_to.window(handles[-1])
|
| 1401 |
+
screenshot = driver.get_screenshot_as_base64()
|
| 1402 |
+
except Exception:
|
| 1403 |
+
pass
|
| 1404 |
|
| 1405 |
current_url = ""
|
| 1406 |
title = ""
|
|
|
|
| 1413 |
session["last_url"] = current_url
|
| 1414 |
session["last_activity"] = time.time()
|
| 1415 |
|
| 1416 |
+
url_changed = current_url != original_url
|
| 1417 |
+
|
| 1418 |
return {
|
| 1419 |
"success": True,
|
| 1420 |
"message": f"Clique em ({actual_x}, {actual_y})",
|
|
|
|
| 1423 |
"url": current_url,
|
| 1424 |
"current_url": current_url,
|
| 1425 |
"title": title,
|
| 1426 |
+
"screenshot": screenshot,
|
| 1427 |
+
"url_changed": url_changed
|
| 1428 |
}
|
| 1429 |
|
| 1430 |
except HTTPException:
|
| 1431 |
raise
|
| 1432 |
except Exception as e:
|
| 1433 |
logger.error(f"Erro no clique: {e}")
|
| 1434 |
+
# Tentar screenshot mesmo com erro
|
| 1435 |
+
screenshot = None
|
| 1436 |
+
try:
|
| 1437 |
+
driver = session_manager.get_session(request.get("session_id", ""))
|
| 1438 |
+
if driver:
|
| 1439 |
+
d = driver["driver"]
|
| 1440 |
+
handles = d.window_handles
|
| 1441 |
+
if handles:
|
| 1442 |
+
d.switch_to.window(handles[-1])
|
| 1443 |
+
screenshot = d.get_screenshot_as_base64()
|
| 1444 |
+
except Exception:
|
| 1445 |
+
pass
|
| 1446 |
+
return {"success": False, "message": str(e), "screenshot": screenshot}
|
| 1447 |
# ============================================================
|
| 1448 |
# SEARCH SITE
|
| 1449 |
# ============================================================
|