diff --git a/DrissionPage/chromium_element.py b/DrissionPage/chromium_element.py index 8a67c9f..b2c72ae 100644 --- a/DrissionPage/chromium_element.py +++ b/DrissionPage/chromium_element.py @@ -498,15 +498,10 @@ class ChromiumElement(DrissionElement): if self.tag == 'input' and self.attr('type') == 'file': return self._set_file_input(vals) - try: - self.page.run_cdp('DOM.focus', backendNodeId=self._backend_id) - except Exception: - pass - self.click(by_js=True) - self.click.at() - if clear and vals != '\n': self.clear(by_js=False) + else: + self._focus() # ------------处理字符------------- if not isinstance(vals, (tuple, list)): @@ -533,8 +528,16 @@ class ChromiumElement(DrissionElement): self.run_js("this.value='';") else: + self._focus() self.input(('\ue009', 'a', '\ue017'), clear=False) + def _focus(self): + """使元素获取焦点""" + try: + self.page.run_cdp('DOM.focus', backendNodeId=self._backend_id) + except Exception: + self.run_js('this.focus();') + def hover(self, offset_x=None, offset_y=None): """鼠标悬停,可接受偏移量,偏移量相对于元素左上角坐标。不传入x或y值时悬停在元素中点 :param offset_x: 相对元素左上角坐标的x轴偏移量 diff --git a/DrissionPage/chromium_element.pyi b/DrissionPage/chromium_element.pyi index 00bd613..c14315c 100644 --- a/DrissionPage/chromium_element.pyi +++ b/DrissionPage/chromium_element.pyi @@ -180,6 +180,8 @@ class ChromiumElement(DrissionElement): def clear(self, by_js: bool = False) -> None: ... + def _focus(self) -> None: ... + def hover(self, offset_x: int = None, offset_y: int = None) -> None: ... def drag(self, offset_x: int = 0, offset_y: int = 0, speed: int = 40) -> None: ... @@ -439,10 +441,6 @@ class Click(object): def middle(self): ... - def left_at(self, offset_x: int = None, offset_y: int = None) -> None: ... - - def right_at(self, offset_x: int = None, offset_y: int = None) -> None: ... - def at(self, offset_x: int = None, offset_y: int = None, button='left') -> None: ... def _click(self, client_x: int, client_y: int, button: str = 'left') -> None: ...