From 3005468ce070763a4f553c21def614a48926dc31 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8F=88=E9=98=B3=E6=9C=88?= <2497732985@qq.com> Date: Sat, 30 Dec 2023 00:47:59 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=8E=A5=E5=8F=A3=20add?= =?UTF-8?q?=5Finit=5Fscript=EF=BC=9A=E5=9C=A8=E9=A1=B5=E9=9D=A2=E8=BF=9B?= =?UTF-8?q?=E8=A1=8C=E4=BB=BB=E4=BD=95=E8=84=9A=E6=9C=AC=E7=9A=84=E5=8A=A0?= =?UTF-8?q?=E8=BD=BD=E5=89=8D=EF=BC=8C=E6=89=A7=E8=A1=8Cjs=E8=AF=AD?= =?UTF-8?q?=E5=8F=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DrissionPage/_pages/chromium_base.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/DrissionPage/_pages/chromium_base.py b/DrissionPage/_pages/chromium_base.py index 9ca83ed..8ff490c 100644 --- a/DrissionPage/_pages/chromium_base.py +++ b/DrissionPage/_pages/chromium_base.py @@ -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 From 290baa680a23529029c77bf93570ac8ea3bf8bae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8F=88=E9=98=B3=E6=9C=88?= <2497732985@qq.com> Date: Sat, 30 Dec 2023 12:19:05 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E7=A7=BB=E5=8A=A8add=5Finit=5Fscript?= =?UTF-8?q?=E5=88=B0TabSetter=E4=B8=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DrissionPage/_pages/chromium_base.py | 13 ------------- DrissionPage/_units/setter.py | 14 ++++++++++++++ 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/DrissionPage/_pages/chromium_base.py b/DrissionPage/_pages/chromium_base.py index 8ff490c..48b818a 100644 --- a/DrissionPage/_pages/chromium_base.py +++ b/DrissionPage/_pages/chromium_base.py @@ -492,19 +492,6 @@ class ChromiumBase(BasePage): :return: None """ 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 diff --git a/DrissionPage/_units/setter.py b/DrissionPage/_units/setter.py index 5c27059..683ee10 100644 --- a/DrissionPage/_units/setter.py +++ b/DrissionPage/_units/setter.py @@ -10,6 +10,7 @@ from requests.structures import CaseInsensitiveDict from .cookies_setter import SessionCookiesSetter, CookiesSetter from .._functions.tools import show_or_hide_browser +__ERROR__ = 'error' class BasePageSetter(object): def __init__(self, page): @@ -198,6 +199,19 @@ class TabSetter(ChromiumBaseSetter): """使标签页处于最前面""" self._page.browser.activate_tab(self._page.tab_id) + 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 + class ChromiumPageSetter(TabSetter):