Files
wiki/playwright
2024-12-24 07:11:58 +00:00
..
2024-08-11 14:01:10 +09:00
2024-08-24 15:21:31 -04:00
2024-09-01 14:48:03 +09:00
2024-12-24 07:11:58 +00:00

"""
Linux (WSL Debian)

pip install playwright
playwright install --with-deps
"""
def sync(f):
    import asyncio, threading, queue, functools; asyncio.set_event_loop_policy(None)
    def thread(): sync.q.get(); f, a, k = sync.p; sync.p = f(*a, **k); sync.q.task_done(); thread()
    if not hasattr(sync, 'q'): sync.q = queue.Queue(); threading.Thread(target=thread).start()
    def wrapper(*a, **k): sync.p = f, a, k; sync.q.put(1); sync.q.join(); return sync.p
    return functools.wraps(f)(wrapper)
@sync
def Page():
    from playwright.sync_api import sync_playwright as P
    (context := P().start().firefox.launch().new_context()).set_default_timeout(0)
    for attr in dir(page := context.new_page()):
        if attr[0] != '_' and callable(method := getattr(page, attr)): setattr(page, attr, sync(method))
    page._repr_png_ = page.screenshot; return page
(page := Page()).goto('https://example.org')
print(page.title()) # Example Domain