diff --git a/DrissionPage/chromium_base.py b/DrissionPage/chromium_base.py index 8719d4a..7ed9c76 100644 --- a/DrissionPage/chromium_base.py +++ b/DrissionPage/chromium_base.py @@ -1061,13 +1061,28 @@ class ChromiumBaseWaiter(object): """ return self._loading(timeout=timeout, start=False) - def data_package(self, target, timeout=None): + def new_frame(self, timeout=None): + """等待新frame加载到dom + :param timeout: 超时时间 + :return: 是否等待成功 """ - :param target: - :param timeout: - :return: - """ - pass + timeout = timeout if timeout is not None else self._driver.timeout + self._new_frame = False + self._driver.driver.Page.frameAttached = self._on_frame_attached + result = False + end_time = perf_counter() + timeout + while perf_counter() < end_time: + if self._new_frame: + result = True + break + sleep(.1) + + self._driver.driver.Page.frameAttached = None + self._new_frame = False + return result + + def _on_frame_attached(self): + self._new_frame = True def upload_paths_inputted(self): """等待自动填写上传文件路径""" @@ -1090,6 +1105,53 @@ class ChromiumBaseWaiter(object): sleep(gap) return False + # def data_package(self, target, timeout=None): + # """ + # :param target: + # :param timeout: + # :return: + # """ + # self._target = target + # self._request_id = None + # timeout = timeout if timeout is not None else self._driver.timeout + # end_time = perf_counter() + timeout + # while perf_counter() < end_time: + # pass + # + # def _response_received(self, **kwargs): + # """接收到返回信息时处理方法""" + # if self._target in kwargs['response']['url']: + # + # + # if self.targets is True: + # self._request_ids[kwargs['requestId']] = {'target': True, 'response': kwargs['response']} + # + # else: + # for target in self.targets: + # if target in kwargs['response']['url']: + # self._request_ids[kwargs['requestId']] = {'target': target, 'response': kwargs['response']} + # + # def _loading_finished(self, **kwargs): + # """请求完成时处理方法""" + # if not self._is_continue(): + # return + # + # request_id = kwargs['requestId'] + # target = self._request_ids.pop(request_id, None) + # if target is None: + # return + # + # target, response = target.values() + # response = ResponseData(request_id, response, self._get_response_body(request_id), self.tab_id, target) + # response.postData = self._get_post_data(request_id) + # + # self._caught_count += 1 + # self._tmp.put(response) + # self.results.append(response) + # + # if not self._is_continue(): + # self.stop() + class ChromiumPageScroll(ChromiumScroll): def __init__(self, page): diff --git a/DrissionPage/chromium_base.pyi b/DrissionPage/chromium_base.pyi index 39b2792..55234b4 100644 --- a/DrissionPage/chromium_base.pyi +++ b/DrissionPage/chromium_base.pyi @@ -217,6 +217,7 @@ class ChromiumBase(BasePage): class ChromiumBaseWaiter(object): def __init__(self, page: ChromiumBase): self._driver: ChromiumBase = ... + self._new_frame: bool = ... def ele_delete(self, loc_or_ele: Union[str, tuple, ChromiumElement], timeout: float = None) -> bool: ... @@ -230,7 +231,11 @@ class ChromiumBaseWaiter(object): def load_complete(self, timeout: float = None) -> bool: ... - def data_package(self, target, timeout: float = None) -> Union[ResponseData, None]: ... + def new_frame(self, timeout: float = None) -> bool: ... + + def _on_frame_attached(self): ... + + def data_package(self, target: str, timeout: float = None) -> Union[ResponseData, None]: ... def upload_paths_inputted(self) -> None: ... diff --git a/DrissionPage/chromium_element.py b/DrissionPage/chromium_element.py index 7ae2846..48e7ad9 100644 --- a/DrissionPage/chromium_element.py +++ b/DrissionPage/chromium_element.py @@ -355,7 +355,8 @@ class ChromiumElement(DrissionElement): if 'value' not in i or 'value' not in i['value']: return None - return format_html(i['value']['value']) + value = i['value']['value'] + return format_html(value) if isinstance(value, str) else value def run_js(self, script, *args, as_expr=False): """对本元素执行javascript代码 @@ -1724,22 +1725,20 @@ class Click(object): """ self._ele = ele - def __call__(self, by_js=False, timeout=1, wait_loading=0): + def __call__(self, by_js=False, timeout=1): """点击元素 如果遇到遮挡,可选择是否用js点击 :param by_js: 是否用js点击,为None时先用模拟点击,遇到遮挡改用js,为True时直接用js点击,为False时只用模拟点击 :param timeout: 模拟点击的超时时间,等待元素可见、不被遮挡、进入视口 - :param wait_loading: 等待页面进入加载状态超时时间 :return: 是否点击成功 """ - return self.left(by_js, timeout, wait_loading) + return self.left(by_js, timeout) - def left(self, by_js=False, timeout=1, wait_loading=0): + def left(self, by_js=False, timeout=1): """点击元素 如果遇到遮挡,可选择是否用js点击 :param by_js: 是否用js点击,为None时先用模拟点击,遇到遮挡改用js,为True时直接用js点击,为False时只用模拟点击 :param timeout: 模拟点击的超时时间,等待元素可见、不被遮挡、进入视口 - :param wait_loading: 等待页面进入加载状态超时时间 :return: 是否点击成功 """ if not by_js: @@ -1765,7 +1764,6 @@ class Click(object): client_x, client_y = self._ele.locations.viewport_midpoint if self._ele.tag == 'input' \ else self._ele.locations.viewport_click_point self._click(client_x, client_y) - self._ele.page.wait.load_start(wait_loading) return True except NoRectError: @@ -1773,7 +1771,6 @@ class Click(object): if by_js is not False: self._ele.run_js('this.click();') - self._ele.page.wait.load_start(wait_loading) return True return False diff --git a/DrissionPage/chromium_element.pyi b/DrissionPage/chromium_element.pyi index 84be5f5..f7bd4ba 100644 --- a/DrissionPage/chromium_element.pyi +++ b/DrissionPage/chromium_element.pyi @@ -445,9 +445,9 @@ class Click(object): def __init__(self, ele: ChromiumElement): self._ele: ChromiumElement = ... - def __call__(self, by_js: bool = False, timeout: float = 1, wait_loading: Union[bool, float] = 0) -> bool: ... + def __call__(self, by_js: bool = False, timeout: float = 1) -> bool: ... - def left(self, by_js: bool = False, timeout: float = 1, wait_loading: Union[bool, float] = 0) -> bool: ... + def left(self, by_js: bool = False, timeout: float = 1) -> bool: ... def right(self): ... diff --git a/DrissionPage/chromium_frame.py b/DrissionPage/chromium_frame.py index 0d0387b..1de0946 100644 --- a/DrissionPage/chromium_frame.py +++ b/DrissionPage/chromium_frame.py @@ -121,7 +121,7 @@ class ChromiumFrame(ChromiumBase): break except Exception: - pass + sleep(.1) if self._debug: print('---获取document结束') @@ -275,7 +275,7 @@ class ChromiumFrame(ChromiumBase): try: return self.doc_ele.run_js('return this.readyState;') except: - pass + sleep(.1) @property def scroll(self): diff --git a/DrissionPage/commons/browser.py b/DrissionPage/commons/browser.py index 2943609..38e565e 100644 --- a/DrissionPage/commons/browser.py +++ b/DrissionPage/commons/browser.py @@ -171,7 +171,9 @@ def _run_browser(port, path: str, args) -> Popen: :param args: 启动参数 :return: 进程对象 """ - arguments = [path, f'--remote-debugging-port={port}'] + p = Path(path) + p = str(p / 'chrome.exe') if p.is_dir() else str(path) + arguments = [p, f'--remote-debugging-port={port}'] arguments.extend(args) return Popen(arguments, shell=False)