From dcbf9700752143cb34fd6b861268820e2d688e70 Mon Sep 17 00:00:00 2001 From: g1879 Date: Thu, 4 Jan 2024 23:14:24 +0800 Subject: [PATCH] =?UTF-8?q?check()=E5=A2=9E=E5=8A=A0by=5Fjs=E5=8F=82?= =?UTF-8?q?=E6=95=B0=EF=BC=9Binput()=E5=92=8Cclear()by=5Fjs=E6=97=B6?= =?UTF-8?q?=E8=A7=A6=E5=8F=91change=EF=BC=9B=E5=A2=9E=E5=8A=A0set.blocked?= =?UTF-8?q?=5Furls()?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DrissionPage/_elements/chromium_element.py | 21 ++++++++++++++++++--- DrissionPage/_elements/chromium_element.pyi | 2 +- DrissionPage/_units/setter.py | 12 ++++++++++++ DrissionPage/_units/setter.pyi | 2 ++ 4 files changed, 33 insertions(+), 4 deletions(-) diff --git a/DrissionPage/_elements/chromium_element.py b/DrissionPage/_elements/chromium_element.py index 790801f..1c94c37 100644 --- a/DrissionPage/_elements/chromium_element.py +++ b/DrissionPage/_elements/chromium_element.py @@ -201,13 +201,26 @@ class ChromiumElement(DrissionElement): return self._select - def check(self, uncheck=False): + def check(self, uncheck=False, by_js=False): """选中或取消选中当前元素 :param uncheck: 是否取消选中 + :param by_js: 是否用js执行 :return: None """ - js = 'this.checked=false' if uncheck else 'this.checked=true' - self.run_js(js) + is_checked = self.states.is_checked + if by_js: + js = None + if is_checked and uncheck: + js = 'this.checked=false' + elif not is_checked and not uncheck: + js = 'this.checked=true' + if js: + self.run_js(js) + self.run_js('this.dispatchEvent(new Event("change", {bubbles: true}));') + + else: + if (is_checked and uncheck) or (not is_checked and not uncheck): + self.click() def parent(self, level_or_loc=1, index=1): """返回上面某一级父元素,可指定层数或用查询语法定位 @@ -598,6 +611,7 @@ class ChromiumElement(DrissionElement): if isinstance(vals, (list, tuple)): vals = ''.join([str(i) for i in vals]) self.set.prop('value', str(vals)) + self.run_js('this.dispatchEvent(new Event("change", {bubbles: true}));') return if clear and vals not in ('\n', '\ue007'): @@ -614,6 +628,7 @@ class ChromiumElement(DrissionElement): """ if by_js: self.run_js("this.value='';") + self.run_js('this.dispatchEvent(new Event("change", {bubbles: true}));') return self._input_focus() diff --git a/DrissionPage/_elements/chromium_element.pyi b/DrissionPage/_elements/chromium_element.pyi index 4e3f886..9eab619 100644 --- a/DrissionPage/_elements/chromium_element.pyi +++ b/DrissionPage/_elements/chromium_element.pyi @@ -159,7 +159,7 @@ class ChromiumElement(DrissionElement): @property def select(self) -> SelectElement: ... - def check(self, uncheck: bool = False) -> None: ... + def check(self, uncheck: bool = False, by_js: bool = False) -> None: ... def attr(self, attr: str) -> Union[str, None]: ... diff --git a/DrissionPage/_units/setter.py b/DrissionPage/_units/setter.py index 8f8b1e1..8e9ad19 100644 --- a/DrissionPage/_units/setter.py +++ b/DrissionPage/_units/setter.py @@ -145,6 +145,18 @@ class ChromiumBaseSetter(BasePageSetter): """ self._page._alert.auto = accept if on_off else None + def blocked_urls(self, urls): + """设置要忽略的url,传入None时清空已设置的内容。 + :param urls: + :return: None + """ + if not urls: + urls = [] + if not isinstance(urls, (list, tuple)): + raise TypeError('urls需传入list或tuple类型。') + self._page.run_cdp('Network.enable') + self._page.run_cdp('Network.setBlockedURLs', urls=urls) + # --------------即将废弃--------------- @property diff --git a/DrissionPage/_units/setter.pyi b/DrissionPage/_units/setter.pyi index 0ac2f98..f8e5de0 100644 --- a/DrissionPage/_units/setter.pyi +++ b/DrissionPage/_units/setter.pyi @@ -62,6 +62,8 @@ class ChromiumBaseSetter(BasePageSetter): def upload_files(self, files: Union[str, list, tuple]) -> None: ... + def blocked_urls(self, urls: Optional[list, tuple]) -> None: ... + class TabSetter(ChromiumBaseSetter): def __init__(self, page): ...