-
Notifications
You must be signed in to change notification settings - Fork 29
/
demo.py
43 lines (38 loc) · 1.51 KB
/
demo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import asyncio
import json
from ichrome import AsyncChromeDaemon
async def main():
async with AsyncChromeDaemon(
clear_after_shutdown=True,
headless=False,
disable_image=False,
user_data_dir="./ichrome_user_data",
) as cd:
async with cd.connect_tab(0, auto_close=True) as tab:
loaded = await tab.goto("https://httpbin.org/forms/post", timeout=10)
html = await tab.html
title = await tab.title
print(
f'page loaded ok: {loaded}, HTML length is {len(html)}, title is "{title}"'
)
# try setting the input tag value with JS
await tab.js(
r"""document.querySelector('[value="bacon"]').checked = true"""
)
# or you can click the checkbox
await tab.click('[value="cheese"]')
# you can set the value of input
await tab.js(
r'''document.querySelector('[name="custname"]').value = "1234"'''
)
# now click the submit button
await tab.click("form button")
await tab.wait_loading(5)
# extract the JSON with regex
result = await tab.findone(r"<pre.*?>([\s\S]*?)</pre>")
print(json.loads(result))
# ================= now tab will be closed, and cache will be clear =================
# try debugging with repl mode like: tab.url
await tab.repl()
if __name__ == "__main__":
asyncio.run(main())