import subprocess,sys,time,pathlib,hashlib
browser,session,destination=sys.argv[1:]; dest=pathlib.Path(destination)
args=[browser,'--session',session]
prev=None
for attempt in range(12):
 p=dest.with_name(dest.stem+'-candidate.png')
 subprocess.run(args+['screenshot',str(p)],check=True,stdout=subprocess.DEVNULL)
 content=p.read_bytes()
 if content==prev:
  dest.write_bytes(content); print('Stable screenshot:',dest.name); break
 prev=content; time.sleep(.5)
else: raise SystemExit('Screenshots did not stabilize')
