Closed ttygde closed 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}
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.
Thanks!
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}
I'll try this later
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.
It works. Thank you very much!
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}
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
Why add_js_onload is async only? Is there way to do it in sync tab?