check()增加by_js参数;input()和clear()by_js时触发change;增加set.blocked_urls()

This commit is contained in:
g1879 2024-01-04 23:14:24 +08:00
parent f13681e0df
commit dcbf970075
4 changed files with 33 additions and 4 deletions

View File

@ -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()

View File

@ -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]: ...

View File

@ -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

View File

@ -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): ...