添加接口 add_init_script:在页面进行任何脚本的加载前,执行js语句

This commit is contained in:
又阳月 2023-12-30 00:47:59 +08:00
parent 6f5020f955
commit 3005468ce0

View File

@ -493,6 +493,19 @@ class ChromiumBase(BasePage):
"""
run_js(self, script, as_expr, 0, args)
def add_init_script(self, script: str, raise_error=True):
'''添加初始化脚本,在页面加载任何脚本前执行
:param script: js文本
:return: identifier 添加的脚本的标识符用于删除失败时返回False或raise Error
'''
result = self.driver.run('Page.addScriptToEvaluateOnNewDocument', source=script)
if not result or __ERROR__ not in result:
return result['identifier']
else:
if raise_error:
raise_error(str(result))
return False
def get(self, url, show_errmsg=False, retry=None, interval=None, timeout=None):
"""访问url
:param url: 目标url