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

在不同的tab.js()构建的全局变量不通用 #70

Closed bluebad closed 2 years ago

bluebad commented 2 years ago

在同一个tab的两次.js()调用中,给window声明的全局变量不可通用,比如 // 第一次调用 tab.js(window.myslots = {};) // 第二次调用 tab.js(window.myslots['property-key'] = "spam") 结果第二次报错:TypeError: Cannot set properties of undefined.... 不知道这是什么原因?是不是我使用有错误?

ClericPy commented 2 years ago

python3 -m pip install ichrome -U

python3 -m ichrome -t

>>> await tab.js('window.myslots={}')
{'type': 'object', 'className': 'Object', 'description': 'Object', 'objectId': '-5914184365824789682.1.1'}
>>> await tab.js(r'''window.myslots['property-key'] = "spam"''')
{'type': 'string', 'value': 'spam'}
>>> await tab.js('JSON.stringify(window.myslots)')
{'type': 'string', 'value': '{"property-key":"spam"}'}
bluebad commented 2 years ago

感谢,是我的问题,好久不写异步的代码,给流程绕晕了。 原因是:在两次操作js()间隔,页面作了刷新,造成两个window不同。。。好菜的错误。 浪费楼主宝贵时间,抱歉!