mirror of
https://gitee.com/g1879/DrissionPage.git
synced 2024-12-10 04:00:23 +08:00
增加wait.data_packet()方法;提高页面对象查找元素稳定性;删除wait.new_frame()方法
This commit is contained in:
parent
7eb1aac778
commit
a798317ae9
@ -5,13 +5,12 @@
|
|||||||
"""
|
"""
|
||||||
from abc import abstractmethod
|
from abc import abstractmethod
|
||||||
from re import sub
|
from re import sub
|
||||||
from time import sleep
|
|
||||||
from urllib.parse import quote
|
from urllib.parse import quote
|
||||||
|
|
||||||
from .commons.constants import Settings, NoneElement
|
from .commons.constants import Settings, NoneElement
|
||||||
from .commons.locator import get_loc
|
from .commons.locator import get_loc
|
||||||
from .commons.web import format_html
|
from .commons.web import format_html
|
||||||
from .errors import ElementNotFoundError, ContextLossError
|
from .errors import ElementNotFoundError
|
||||||
|
|
||||||
|
|
||||||
class BaseParser(object):
|
class BaseParser(object):
|
||||||
@ -72,13 +71,7 @@ class BaseElement(BaseParser):
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
def _ele(self, loc_or_str, timeout=None, single=True, relative=False, raise_err=None):
|
def _ele(self, loc_or_str, timeout=None, single=True, relative=False, raise_err=None):
|
||||||
while True:
|
r = self._find_elements(loc_or_str, timeout=timeout, single=single, relative=relative, raise_err=raise_err)
|
||||||
try:
|
|
||||||
r = self._find_elements(loc_or_str, timeout=timeout, single=single,
|
|
||||||
relative=relative, raise_err=raise_err)
|
|
||||||
break
|
|
||||||
except ContextLossError:
|
|
||||||
sleep(.1)
|
|
||||||
if not single or raise_err is False:
|
if not single or raise_err is False:
|
||||||
return r
|
return r
|
||||||
if not r and (Settings.raise_ele_not_found or raise_err is True):
|
if not r and (Settings.raise_ele_not_found or raise_err is True):
|
||||||
@ -420,12 +413,7 @@ class BasePage(BaseParser):
|
|||||||
if not loc_or_ele:
|
if not loc_or_ele:
|
||||||
raise ElementNotFoundError
|
raise ElementNotFoundError
|
||||||
|
|
||||||
while True:
|
|
||||||
try:
|
|
||||||
r = self._find_elements(loc_or_ele, timeout=timeout, single=single, raise_err=raise_err)
|
r = self._find_elements(loc_or_ele, timeout=timeout, single=single, raise_err=raise_err)
|
||||||
break
|
|
||||||
except ContextLossError:
|
|
||||||
sleep(.1)
|
|
||||||
|
|
||||||
if not single or raise_err is False:
|
if not single or raise_err is False:
|
||||||
return r
|
return r
|
||||||
|
@ -11,6 +11,7 @@ from threading import Thread
|
|||||||
from time import perf_counter, sleep, time
|
from time import perf_counter, sleep, time
|
||||||
from warnings import warn
|
from warnings import warn
|
||||||
|
|
||||||
|
from FlowViewer.listener import ResponseData
|
||||||
from requests import Session
|
from requests import Session
|
||||||
|
|
||||||
from .base import BasePage
|
from .base import BasePage
|
||||||
@ -507,8 +508,12 @@ class ChromiumBase(BasePage):
|
|||||||
timeout = timeout if timeout is not None else self.timeout
|
timeout = timeout if timeout is not None else self.timeout
|
||||||
end_time = perf_counter() + timeout
|
end_time = perf_counter() + timeout
|
||||||
|
|
||||||
|
try:
|
||||||
search_result = self.run_cdp_loaded('DOM.performSearch', query=loc, includeUserAgentShadowDOM=True)
|
search_result = self.run_cdp_loaded('DOM.performSearch', query=loc, includeUserAgentShadowDOM=True)
|
||||||
count = search_result['resultCount']
|
count = search_result['resultCount']
|
||||||
|
except ContextLossError:
|
||||||
|
search_result = None
|
||||||
|
count = 0
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
if count > 0:
|
if count > 0:
|
||||||
@ -520,7 +525,7 @@ class ChromiumBase(BasePage):
|
|||||||
ok = True
|
ok = True
|
||||||
|
|
||||||
except Exception:
|
except Exception:
|
||||||
sleep(.01)
|
pass
|
||||||
|
|
||||||
if ok:
|
if ok:
|
||||||
try:
|
try:
|
||||||
@ -532,12 +537,17 @@ class ChromiumBase(BasePage):
|
|||||||
except ElementLossError:
|
except ElementLossError:
|
||||||
ok = False
|
ok = False
|
||||||
|
|
||||||
|
try:
|
||||||
search_result = self.run_cdp_loaded('DOM.performSearch', query=loc, includeUserAgentShadowDOM=True)
|
search_result = self.run_cdp_loaded('DOM.performSearch', query=loc, includeUserAgentShadowDOM=True)
|
||||||
count = search_result['resultCount']
|
count = search_result['resultCount']
|
||||||
|
except ContextLossError:
|
||||||
|
pass
|
||||||
|
|
||||||
if perf_counter() >= end_time:
|
if perf_counter() >= end_time:
|
||||||
return NoneElement() if single else []
|
return NoneElement() if single else []
|
||||||
|
|
||||||
|
sleep(.1)
|
||||||
|
|
||||||
def refresh(self, ignore_cache=False):
|
def refresh(self, ignore_cache=False):
|
||||||
"""刷新当前页面
|
"""刷新当前页面
|
||||||
:param ignore_cache: 是否忽略缓存
|
:param ignore_cache: 是否忽略缓存
|
||||||
@ -1082,52 +1092,51 @@ class ChromiumBaseWaiter(object):
|
|||||||
sleep(gap)
|
sleep(gap)
|
||||||
return False
|
return False
|
||||||
|
|
||||||
# def data_package(self, target, timeout=None):
|
def set_target(self, target):
|
||||||
# """
|
"""指定要等待的数据包
|
||||||
# :param target:
|
:param target:
|
||||||
# :param timeout:
|
:return: None
|
||||||
# :return:
|
"""
|
||||||
# """
|
self._target = target
|
||||||
# self._target = target
|
self._driver.run_cdp('Network.enable')
|
||||||
# self._request_id = None
|
self._driver.driver.Network.responseReceived = self._response_received
|
||||||
# timeout = timeout if timeout is not None else self._driver.timeout
|
self._driver.driver.Network.loadingFinished = self._loading_finished
|
||||||
# end_time = perf_counter() + timeout
|
|
||||||
# while perf_counter() < end_time:
|
def data_packet(self, target=None, timeout=None):
|
||||||
# pass
|
"""等待指定数据包加载完成
|
||||||
#
|
:param target: 指定的数据包url片段
|
||||||
# def _response_received(self, **kwargs):
|
:param timeout: 超时时间,为None则使用页面对象timeout
|
||||||
# """接收到返回信息时处理方法"""
|
:return: ResponseData对象
|
||||||
# if self._target in kwargs['response']['url']:
|
"""
|
||||||
#
|
if target:
|
||||||
#
|
self._target = target
|
||||||
# if self.targets is True:
|
self._request_id = None
|
||||||
# self._request_ids[kwargs['requestId']] = {'target': True, 'response': kwargs['response']}
|
self._response_data = None
|
||||||
#
|
|
||||||
# else:
|
timeout = timeout if timeout is not None else self._driver.timeout
|
||||||
# for target in self.targets:
|
end_time = perf_counter() + timeout
|
||||||
# if target in kwargs['response']['url']:
|
while not self._response_data and perf_counter() < end_time:
|
||||||
# self._request_ids[kwargs['requestId']] = {'target': target, 'response': kwargs['response']}
|
sleep(.1)
|
||||||
#
|
|
||||||
# def _loading_finished(self, **kwargs):
|
self._request_id = None
|
||||||
# """请求完成时处理方法"""
|
|
||||||
# if not self._is_continue():
|
return self._response_data or False
|
||||||
# return
|
|
||||||
#
|
def _response_received(self, **kwargs):
|
||||||
# request_id = kwargs['requestId']
|
"""接收到返回信息时处理方法"""
|
||||||
# target = self._request_ids.pop(request_id, None)
|
if self._target in kwargs['response']['url']:
|
||||||
# if target is None:
|
self._request_id = kwargs['requestId']
|
||||||
# return
|
self._response = kwargs['response']
|
||||||
#
|
|
||||||
# target, response = target.values()
|
def _loading_finished(self, **kwargs):
|
||||||
# response = ResponseData(request_id, response, self._get_response_body(request_id), self.tab_id, target)
|
"""请求完成时处理方法"""
|
||||||
# response.postData = self._get_post_data(request_id)
|
if kwargs['requestId'] == self._request_id:
|
||||||
#
|
try:
|
||||||
# self._caught_count += 1
|
body = self._driver.run_cdp('Network.getResponseBody', requestId=self._request_id)['body']
|
||||||
# self._tmp.put(response)
|
except:
|
||||||
# self.results.append(response)
|
body = ''
|
||||||
#
|
self._response_data = ResponseData(self._request_id, self._response, body,
|
||||||
# if not self._is_continue():
|
self._driver.tab_id, self._target)
|
||||||
# self.stop()
|
|
||||||
|
|
||||||
|
|
||||||
class ChromiumPageScroll(ChromiumScroll):
|
class ChromiumPageScroll(ChromiumScroll):
|
||||||
|
@ -217,6 +217,10 @@ class ChromiumBase(BasePage):
|
|||||||
class ChromiumBaseWaiter(object):
|
class ChromiumBaseWaiter(object):
|
||||||
def __init__(self, page: ChromiumBase):
|
def __init__(self, page: ChromiumBase):
|
||||||
self._driver: ChromiumBase = ...
|
self._driver: ChromiumBase = ...
|
||||||
|
self._target: str = ...
|
||||||
|
self._request_id: str = ...
|
||||||
|
self._response: dict = ...
|
||||||
|
self._response_data: ResponseData = ...
|
||||||
|
|
||||||
def ele_delete(self, loc_or_ele: Union[str, tuple, ChromiumElement], timeout: float = None) -> bool: ...
|
def ele_delete(self, loc_or_ele: Union[str, tuple, ChromiumElement], timeout: float = None) -> bool: ...
|
||||||
|
|
||||||
@ -230,7 +234,13 @@ class ChromiumBaseWaiter(object):
|
|||||||
|
|
||||||
def load_complete(self, timeout: float = None) -> bool: ...
|
def load_complete(self, timeout: float = None) -> bool: ...
|
||||||
|
|
||||||
def data_package(self, target: str, timeout: float = None) -> Union[ResponseData, None]: ...
|
def set_target(self, target: str) -> None: ...
|
||||||
|
|
||||||
|
def data_packet(self, target: str = None, timeout: float = None) -> Union[ResponseData, None]: ...
|
||||||
|
|
||||||
|
def _response_received(self, **kwargs): ...
|
||||||
|
|
||||||
|
def _loading_finished(self, **kwargs): ...
|
||||||
|
|
||||||
def upload_paths_inputted(self) -> None: ...
|
def upload_paths_inputted(self) -> None: ...
|
||||||
|
|
||||||
|
@ -1252,19 +1252,16 @@ def find_by_xpath(ele, xpath, single, timeout, relative=True):
|
|||||||
type_txt = '9' if single else '7'
|
type_txt = '9' if single else '7'
|
||||||
node_txt = 'this.contentDocument' if ele.tag in FRAME_ELEMENT and not relative else 'this'
|
node_txt = 'this.contentDocument' if ele.tag in FRAME_ELEMENT and not relative else 'this'
|
||||||
js = make_js_for_find_ele_by_xpath(xpath, type_txt, node_txt)
|
js = make_js_for_find_ele_by_xpath(xpath, type_txt, node_txt)
|
||||||
r = ele.page.run_cdp_loaded('Runtime.callFunctionOn',
|
r = ele.page.run_cdp_loaded('Runtime.callFunctionOn', functionDeclaration=js, objectId=ele.ids.obj_id,
|
||||||
functionDeclaration=js, objectId=ele.ids.obj_id, returnByValue=False, awaitPromise=True,
|
returnByValue=False, awaitPromise=True, userGesture=True)
|
||||||
userGesture=True)
|
|
||||||
if r['result']['type'] == 'string':
|
if r['result']['type'] == 'string':
|
||||||
return r['result']['value']
|
return r['result']['value']
|
||||||
|
|
||||||
if 'exceptionDetails' in r:
|
if 'exceptionDetails' in r:
|
||||||
if 'The result is not a node set' in r['result']['description']:
|
if 'The result is not a node set' in r['result']['description']:
|
||||||
js = make_js_for_find_ele_by_xpath(xpath, '1', node_txt)
|
js = make_js_for_find_ele_by_xpath(xpath, '1', node_txt)
|
||||||
r = ele.page.run_cdp_loaded('Runtime.callFunctionOn',
|
r = ele.page.run_cdp_loaded('Runtime.callFunctionOn', functionDeclaration=js, objectId=ele.ids.obj_id,
|
||||||
functionDeclaration=js, objectId=ele.ids.obj_id, returnByValue=False,
|
returnByValue=False, awaitPromise=True, userGesture=True)
|
||||||
awaitPromise=True,
|
|
||||||
userGesture=True)
|
|
||||||
return r['result']['value']
|
return r['result']['value']
|
||||||
else:
|
else:
|
||||||
raise SyntaxError(f'查询语句错误:\n{r}')
|
raise SyntaxError(f'查询语句错误:\n{r}')
|
||||||
@ -1272,9 +1269,8 @@ def find_by_xpath(ele, xpath, single, timeout, relative=True):
|
|||||||
end_time = perf_counter() + timeout
|
end_time = perf_counter() + timeout
|
||||||
while (r['result']['subtype'] == 'null'
|
while (r['result']['subtype'] == 'null'
|
||||||
or r['result']['description'] == 'NodeList(0)') and perf_counter() < end_time:
|
or r['result']['description'] == 'NodeList(0)') and perf_counter() < end_time:
|
||||||
r = ele.page.run_cdp_loaded('Runtime.callFunctionOn',
|
r = ele.page.run_cdp_loaded('Runtime.callFunctionOn', functionDeclaration=js, objectId=ele.ids.obj_id,
|
||||||
functionDeclaration=js, objectId=ele.ids.obj_id, returnByValue=False, awaitPromise=True,
|
returnByValue=False, awaitPromise=True, userGesture=True)
|
||||||
userGesture=True)
|
|
||||||
|
|
||||||
if single:
|
if single:
|
||||||
return NoneElement() if r['result']['subtype'] == 'null' \
|
return NoneElement() if r['result']['subtype'] == 'null' \
|
||||||
@ -1283,7 +1279,8 @@ def find_by_xpath(ele, xpath, single, timeout, relative=True):
|
|||||||
if r['result']['description'] == 'NodeList(0)':
|
if r['result']['description'] == 'NodeList(0)':
|
||||||
return []
|
return []
|
||||||
else:
|
else:
|
||||||
r = ele.page.run_cdp_loaded('Runtime.getProperties', objectId=r['result']['objectId'], ownProperties=True)['result']
|
r = ele.page.run_cdp_loaded('Runtime.getProperties', objectId=r['result']['objectId'],
|
||||||
|
ownProperties=True)['result']
|
||||||
return [make_chromium_ele(ele.page, obj_id=i['value']['objectId'])
|
return [make_chromium_ele(ele.page, obj_id=i['value']['objectId'])
|
||||||
if i['value']['type'] == 'object' else i['value']['value']
|
if i['value']['type'] == 'object' else i['value']['value']
|
||||||
for i in r[:-1]]
|
for i in r[:-1]]
|
||||||
@ -1301,16 +1298,14 @@ def find_by_css(ele, selector, single, timeout):
|
|||||||
find_all = '' if single else 'All'
|
find_all = '' if single else 'All'
|
||||||
node_txt = 'this.contentDocument' if ele.tag in ('iframe', 'frame', 'shadow-root') else 'this'
|
node_txt = 'this.contentDocument' if ele.tag in ('iframe', 'frame', 'shadow-root') else 'this'
|
||||||
js = f'function(){{return {node_txt}.querySelector{find_all}("{selector}");}}'
|
js = f'function(){{return {node_txt}.querySelector{find_all}("{selector}");}}'
|
||||||
r = ele.page.run_cdp_loaded('Runtime.callFunctionOn',
|
r = ele.page.run_cdp_loaded('Runtime.callFunctionOn', functionDeclaration=js, objectId=ele.ids.obj_id,
|
||||||
functionDeclaration=js, objectId=ele.ids.obj_id, returnByValue=False, awaitPromise=True,
|
returnByValue=False, awaitPromise=True, userGesture=True)
|
||||||
userGesture=True)
|
|
||||||
|
|
||||||
end_time = perf_counter() + timeout
|
end_time = perf_counter() + timeout
|
||||||
while ('exceptionDetails' in r or r['result']['subtype'] == 'null'
|
while ('exceptionDetails' in r or r['result']['subtype'] == 'null' or
|
||||||
or r['result']['description'] == 'NodeList(0)') and perf_counter() < end_time:
|
r['result']['description'] == 'NodeList(0)') and perf_counter() < end_time:
|
||||||
r = ele.page.run_cdp_loaded('Runtime.callFunctionOn',
|
r = ele.page.run_cdp_loaded('Runtime.callFunctionOn', functionDeclaration=js, objectId=ele.ids.obj_id,
|
||||||
functionDeclaration=js, objectId=ele.ids.obj_id, returnByValue=False, awaitPromise=True,
|
returnByValue=False, awaitPromise=True, userGesture=True)
|
||||||
userGesture=True)
|
|
||||||
|
|
||||||
if 'exceptionDetails' in r:
|
if 'exceptionDetails' in r:
|
||||||
raise SyntaxError(f'查询语句错误:\n{r}')
|
raise SyntaxError(f'查询语句错误:\n{r}')
|
||||||
@ -1322,7 +1317,8 @@ def find_by_css(ele, selector, single, timeout):
|
|||||||
if r['result']['description'] == 'NodeList(0)':
|
if r['result']['description'] == 'NodeList(0)':
|
||||||
return []
|
return []
|
||||||
else:
|
else:
|
||||||
r = ele.page.run_cdp_loaded('Runtime.getProperties', objectId=r['result']['objectId'], ownProperties=True)['result']
|
r = ele.page.run_cdp_loaded('Runtime.getProperties', objectId=r['result']['objectId'],
|
||||||
|
ownProperties=True)['result']
|
||||||
return [make_chromium_ele(ele.page, obj_id=i['value']['objectId']) for i in r]
|
return [make_chromium_ele(ele.page, obj_id=i['value']['objectId']) for i in r]
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user