ClericPy / ichrome

Chrome controller for Humans, based on Chrome Devtools Protocol(CDP) and python3.7+.
https://pypi.org/project/ichrome/
MIT License
228 stars 29 forks source link

sync onload #63

Closed ttygde closed 2 years ago

ttygde commented 2 years ago

Why add_js_onload is async only? Is there way to do it in sync tab?

ttygde commented 2 years ago

and using send command directly doesnt work, what am I missing?

chrome.tabs[0].send('Page.addScriptToEvaluateOnNewDocument', source="alert(0);")

INFO  2021-10-05 05:06:28 [ichrome] sync_utils.py(247): <Tab(chrome://newtab/)> send: {'method': 'Page.addScriptToEvaluateOnNewDocument', 'params': {'source': 'alert(0);'}, 'id': 1}
INFO  2021-10-05 05:06:28 [ichrome] sync_utils.py(247): <Tab(chrome://newtab/)> send: {'method': 'Page.enable', 'params': {}, 'id': 1}
INFO  2021-10-05 05:06:28 [ichrome] sync_utils.py(247): <Tab(https://bing.com)> send: {'method': 'Page.navigate', 'params': {'url': 'https://bing.com'}, 'id': 2}
INFO  2021-10-05 05:06:28 [ichrome] sync_utils.py(247): <Tab(https://bing.com)> send: {'method': 'Page.enable', 'params': {}, 'id': 3}
ClericPy commented 2 years ago

Why add_js_onload is async only? Is there way to do it in sync tab?

sync utils is deprecated in ichrome for no time to support two context (my bad, I should have abstracted the connector more completely).

and using send command directly doesnt work, what am I missing?

chrome.tabs[0].send('Page.addScriptToEvaluateOnNewDocument', source="alert(0);")

INFO  2021-10-05 05:06:28 [ichrome] sync_utils.py(247): <Tab(chrome://newtab/)> send: {'method': 'Page.addScriptToEvaluateOnNewDocument', 'params': {'source': 'alert(0);'}, 'id': 1}
INFO  2021-10-05 05:06:28 [ichrome] sync_utils.py(247): <Tab(chrome://newtab/)> send: {'method': 'Page.enable', 'params': {}, 'id': 1}
INFO  2021-10-05 05:06:28 [ichrome] sync_utils.py(247): <Tab(https://bing.com)> send: {'method': 'Page.navigate', 'params': {'url': 'https://bing.com'}, 'id': 2}
INFO  2021-10-05 05:06:28 [ichrome] sync_utils.py(247): <Tab(https://bing.com)> send: {'method': 'Page.enable', 'params': {}, 'id': 3}

Page.enable should be activated before other methods of Page.

PS: maybe you can check microsoft's https://github.com/microsoft/playwright-python for the sync mode usage.

ttygde commented 2 years ago

Thanks!

ttygde commented 2 years ago

Still can't get it working.

browser = Chrome(host="127.0.0.1", port=9222, timeout=3, retry=1)
browser.tabs[0].send('Page.enable')
source = 'console.log("injected"); alert(0);'
browser.tabs[0].send('Page.addScriptToEvaluateOnNewDocument', source=source)
browser.tabs[0].set_url('https://bing.com', timeout=5)

INFO  2021-10-06 05:36:26 [ichrome] sync_utils.py(247): <Tab(chrome://newtab/)> send: {'method': 'Page.enable', 'params': {}, 'id': 1}
INFO  2021-10-06 05:36:26 [ichrome] sync_utils.py(247): <Tab(chrome://newtab/)> send: {'method': 'Page.addScriptToEvaluateOnNewDocument', 'params': {'source': 'console.log("injected"); alert(0);'}, 'id': 1}
INFO  2021-10-06 05:36:26 [ichrome] sync_utils.py(247): <Tab(chrome://newtab/)> send: {'method': 'Page.enable', 'params': {}, 'id': 1}
INFO  2021-10-06 05:36:26 [ichrome] sync_utils.py(247): <Tab(https://bing.com)> send: {'method': 'Page.navigate', 'params': {'url': 'https://bing.com'}, 'id': 2}
INFO  2021-10-06 05:36:28 [ichrome] sync_utils.py(247): <Tab(https://bing.com)> send: {'method': 'Page.enable', 'params': {}, 'id': 3}
ClericPy commented 2 years ago

I'll try this later

ClericPy commented 2 years ago

from ichrome import Chrome

browser = Chrome(host="127.0.0.1", port=9222, timeout=3, retry=1)
tab = browser.tabs[0]
tab.send('Page.enable')
source = 'console.log("injected"); alert(0);'
tab.send('Page.addScriptToEvaluateOnNewDocument', source=source)
tab.set_url('https://bing.com', timeout=5)

tab should not be tabs[0] multi times, because it need to keep only 1 websocket connection.

ttygde commented 2 years ago

It works. Thank you very much!

ttygde commented 2 years ago

I have another problem though. If the tab stays idle for some time(around 20 seconds) it seems that websocket connection is closed and it is stuck there. That's why I used tabs[0] in the first place.

browser = Chrome(host="127.0.0.1", port=9222, timeout=3, retry=1)
tab = self.br.tabs[0]
tab.set_url('https://bing.com', timeout=5)
import time
time.sleep(20)
tab.set_url('https://bing.com', timeout=5)
# got stuck here forever, never returns from above command

INFO  2021-10-07 02:11:55 [ichrome] sync_utils.py(247): <Tab(chrome://newtab/)> send: {'method': 'Page.enable', 'params': {}, 'id': 1}
INFO  2021-10-07 02:11:55 [ichrome] sync_utils.py(247): <Tab(https://bing.com)> send: {'method': 'Page.navigate', 'params': {'url': 'https://bing.com'}, 'id': 2}
INFO  2021-10-07 02:11:55 [ichrome] sync_utils.py(247): <Tab(https://bing.com)> send: {'method': 'Page.enable', 'params': {}, 'id': 3}
INFO  2021-10-07 02:12:15 [ichrome] sync_utils.py(247): <Tab(https://bing.com)> send: {'method': 'Page.enable', 'params': {}, 'id': 4}
INFO  2021-10-07 02:12:15 [ichrome] sync_utils.py(247): <Tab(https://bing.com)> send: {'method': 'Page.navigate', 'params': {'url': 'https://bing.com'}, 'id': 5}
INFO  2021-10-07 02:12:20 [ichrome] sync_utils.py(247): <Tab(https://bing.com)> send: {'method': 'Page.enable', 'params': {}, 'id': 6}
ClericPy commented 2 years ago

Yep you have found a bug long days before, sync_utils has been deprecated so far and not stable.

I will fix that at next version: 2.8.4