diff --git a/DrissionPage/_pages/chromium_base.py b/DrissionPage/_pages/chromium_base.py index c054c6a..1851972 100644 --- a/DrissionPage/_pages/chromium_base.py +++ b/DrissionPage/_pages/chromium_base.py @@ -747,17 +747,6 @@ class ChromiumBase(BasePage): frames = self._ele(locator, timeout=timeout, index=None, raise_err=False) return [i for i in frames if i._type == 'ChromiumFrame'] - def upload(self, loc_or_ele, file_paths, by_js=False): - """触发上传文件选择框并自动填入指定路径 - :param loc_or_ele: 被点击后会触发文件选择框的元素或它的定位符 - :param file_paths: 文件路径,如果上传框支持多文件,可传入列表或字符串,字符串时多个文件用回车分隔 - :param by_js: 是否用js方式点击 - :return: None - """ - self.set.upload_files(file_paths) - self.ele(loc_or_ele).click(by_js=by_js) - self.wait.upload_paths_inputted() - def session_storage(self, item=None): """返回sessionStorage信息,不设置item则获取全部 :param item: 要获取的项,不设置则返回全部 @@ -1147,6 +1136,17 @@ class ChromiumBase(BasePage): def get_cookies(self, as_dict=False, all_domains=False, all_info=False): return self.cookies(as_dict=as_dict, all_domains=all_domains, all_info=all_info) + def upload(self, loc_or_ele, file_paths, by_js=False): + """触发上传文件选择框并自动填入指定路径 + :param loc_or_ele: 被点击后会触发文件选择框的元素或它的定位符 + :param file_paths: 文件路径,如果上传框支持多文件,可传入列表或字符串,字符串时多个文件用回车分隔 + :param by_js: 是否用js方式点击 + :return: None + """ + self.set.upload_files(file_paths) + self.ele(loc_or_ele).click(by_js=by_js) + self.wait.upload_paths_inputted() + class Timeout(object): """用于保存d模式timeout信息的类""" diff --git a/DrissionPage/_pages/chromium_base.pyi b/DrissionPage/_pages/chromium_base.pyi index 3ff2ab1..91e4354 100644 --- a/DrissionPage/_pages/chromium_base.pyi +++ b/DrissionPage/_pages/chromium_base.pyi @@ -223,11 +223,6 @@ class ChromiumBase(BasePage): def get_frames(self, locator: Union[str, tuple] = None, timeout: float = None) -> List[ChromiumFrame]: ... - def upload(self, - loc_or_ele: Union[str, Tuple[str, str], ChromiumElement], - file_paths: Union[str, list, tuple], - by_js: bool = False) -> None: ... - def run_cdp(self, cmd: str, **cmd_args) -> dict: ... def run_cdp_loaded(self, cmd: str, **cmd_args) -> dict: ... diff --git a/DrissionPage/_units/clicker.py b/DrissionPage/_units/clicker.py index 3be5e33..a8b5513 100644 --- a/DrissionPage/_units/clicker.py +++ b/DrissionPage/_units/clicker.py @@ -140,12 +140,13 @@ class Clicker(object): """ self.at(count=times) - def to_download(self, save_path=None, rename=None, suffix=None, new_tab=False): + def to_download(self, save_path=None, rename=None, suffix=None, new_tab=False, by_js=False): """点击触发下载 :param save_path: 保存路径,为None保存在原来设置的,如未设置保存到当前路径 :param rename: 重命名文件名 :param suffix: 指定文件后缀 - :param new_tab: 是否在新tab触发下载 + :param new_tab: 该下载是否在新tab中触发 + :param by_js: 是否用js方式点击,逻辑与click()一致 :return: DownloadMission对象 """ if save_path: @@ -158,9 +159,19 @@ class Clicker(object): tab = self._ele.page._page if new_tab else self._ele.page - self._ele.click() + self._ele.click(by_js=by_js) return tab.wait.download_begin() + def to_upload(self, file_paths, by_js=False): + """触发上传文件选择框并自动填入指定路径 + :param file_paths: 文件路径,如果上传框支持多文件,可传入列表或字符串,字符串时多个文件用回车分隔 + :param by_js: 是否用js方式点击,逻辑与click()一致 + :return: None + """ + self._ele.page.set.upload_files(file_paths) + self._ele.click(by_js=by_js) + self._ele.page.wait.upload_paths_inputted() + def _click(self, client_x, client_y, button='left', count=1): """实施点击 :param client_x: 视口中的x坐标 diff --git a/DrissionPage/_units/clicker.pyi b/DrissionPage/_units/clicker.pyi index 73cc689..7aa346d 100644 --- a/DrissionPage/_units/clicker.pyi +++ b/DrissionPage/_units/clicker.pyi @@ -36,6 +36,9 @@ class Clicker(object): save_path: Union[str, Path] = None, rename: str = None, suffix: str = None, - new_tab: bool = False) -> DownloadMission: ... + new_tab: bool = False, + by_js: bool = False) -> DownloadMission: ... + + def to_upload(self, file_paths: Union[str, list, tuple], by_js: bool = False) -> None: ... def _click(self, client_x: float, client_y: float, button: str = 'left', count: int = 1) -> None: ...