Skip to content

Commit 61fd41f

Browse files
committed
Append to adding scrpae_image and scrape_dom logging
Added scrape_dom and scrape_image to Playwright browser controls; changed log to dump base-64 image to temp not print.
1 parent b7d49a3 commit 61fd41f

2 files changed

Lines changed: 23 additions & 6 deletions

File tree

Project_Andrew/Andrew.rar

2.89 KB
Binary file not shown.

Project_Andrew/os_control.py

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,8 @@ def browser_interact(self, url: str,
338338
elif action == 'scrape_dom':
339339
action_line = "result = page.content()"
340340
elif action == 'scrape_image':
341-
action_line = "result = page.locator(selector).screenshot(type='png', encoding='base64')"
341+
action_line = ("result = page.locator(selector).screenshot(type='png', encoding='base64')\n"
342+
" output_type = 'base64_image'")
342343
elif action == 'screenshot':
343344
action_line = "page.screenshot(path='/tmp/caios_browser.png')"
344345
else:
@@ -347,6 +348,7 @@ def browser_interact(self, url: str,
347348

348349
script = f"""
349350
import sys
351+
import json
350352
try:
351353
from playwright.sync_api import sync_playwright
352354
with sync_playwright() as p:
@@ -358,11 +360,13 @@ def browser_interact(self, url: str,
358360
page.goto('{url}', timeout={timeout * 1000})
359361
{wait_line}
360362
result = ''
363+
output_type = 'text'
361364
{action_line}
362365
if not result:
363366
result = page.url
364367
browser.close()
365-
print(result)
368+
# Output as tagged JSON so caller knows what it received
369+
print(json.dumps({{'output_type': output_type, 'result': result}}))
366370
except ImportError:
367371
print('PLAYWRIGHT_UNAVAILABLE')
368372
except Exception as e:
@@ -377,29 +381,42 @@ def browser_interact(self, url: str,
377381
)
378382

379383
stdout = output.stdout.strip()
380-
381384
# Playwright not installed — fallback to urllib
382385
if stdout == 'PLAYWRIGHT_UNAVAILABLE':
383386
print("[BROWSER] Playwright unavailable — "
384387
"falling back to urllib")
385388
return self.fetch_url(url, extract_mode='full')
386-
387389
if stdout.startswith('ERROR:'):
388390
self._log_action(action_type, url,
389391
f'error: {stdout}')
390392
return {'status': 'error',
391393
'error': stdout[6:]}
392394

395+
# Parse tagged JSON output (scrape_image returns base64)
396+
try:
397+
parsed = json.loads(stdout)
398+
output_type = parsed.get('output_type', 'text')
399+
result = parsed.get('result', stdout)
400+
if output_type == 'base64_image':
401+
import base64
402+
img_path = '/tmp/caios_scrape_image.png'
403+
with open(img_path, 'wb') as f:
404+
f.write(base64.b64decode(result))
405+
result = f'Image saved to {img_path}'
406+
except json.JSONDecodeError:
407+
output_type = 'text'
408+
result = stdout
409+
393410
self._log_action(action_type, url,
394411
f'executed: {action}')
395412
return {
396413
'status': 'success',
397414
'action': action,
398415
'url': url,
399416
'selector': selector,
400-
'result': stdout[:5000]
417+
'output_type': output_type,
418+
'result': result[:5000]
401419
}
402-
403420
except subprocess.TimeoutExpired:
404421
return {'status': 'timeout',
405422
'error': f'Exceeded {timeout}s'}

0 commit comments

Comments
 (0)