38 lines
1.3 KiB
Markdown
38 lines
1.3 KiB
Markdown
```python
|
|
"""
|
|
Linux (WSL Debian)
|
|
|
|
pip install playwright
|
|
playwright install --with-deps
|
|
"""
|
|
def Page():
|
|
def sync(func):
|
|
if not hasattr(sync, 'call'):
|
|
sync.call = __import__('queue').Queue()
|
|
def server():
|
|
while sync.call.get():
|
|
func, args, kwargs = sync.obj
|
|
sync.obj = func(*args, **kwargs)
|
|
sync.call.task_done()
|
|
__import__('asyncio').set_event_loop_policy(None)
|
|
__import__('threading').Thread(target=server).start()
|
|
@__import__('functools').wraps(func)
|
|
def wrapper(*args, **kwargs):
|
|
sync.obj = func, args, kwargs
|
|
sync.call.put(True); sync.call.join()
|
|
return sync.obj
|
|
return wrapper
|
|
@sync
|
|
def Page():
|
|
from playwright.sync_api import sync_playwright
|
|
browser = sync_playwright().start().firefox.launch()
|
|
(context := browser.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
|
|
return Page()
|
|
(page := Page()).goto('https://example.org')
|
|
print(page.title()) # Example Domain
|
|
``` |