4.0.0b30优化_reload();修复type()、select()和close()问题

This commit is contained in:
g1879 2023-12-26 15:51:32 +08:00
parent 465ef15734
commit 23881d03a3
7 changed files with 27 additions and 34 deletions

View File

@ -13,4 +13,4 @@ from ._configs.chromium_options import ChromiumOptions
from ._configs.session_options import SessionOptions
__all__ = ['ChromiumPage', 'ChromiumOptions', 'SessionOptions', 'SessionPage', 'WebPage', '__version__']
__version__ = '4.0.0b29'
__version__ = '4.0.0b30'

View File

@ -92,8 +92,9 @@ class Browser(object):
:param cmd_args: 参数
:return: 执行的结果
"""
ignore = cmd_args.pop('_ignore', None)
r = self._driver.run(cmd, **cmd_args)
return r if __ERROR__ not in r else raise_error(r)
return r if __ERROR__ not in r else raise_error(r, ignore)
@property
def driver(self):

View File

@ -143,7 +143,7 @@ class ChromiumBase(BasePage):
self._driver.set_callback('Page.loadEventFired', self._onLoadEventFired)
self._driver.set_callback('Page.frameStoppedLoading', self._onFrameStoppedLoading)
self._driver.set_callback('Page.frameAttached', self._onFrameAttached)
self._driver.set_callback('Page.frameDetached', self._onFrameDetached)
self._driver.set_callback('Page.frameDetached', self._onFrameDetached, immediate=True)
def _get_document(self, timeout=10):
"""获取页面文档

View File

@ -95,7 +95,7 @@ class ChromiumFrame(ChromiumBase):
except:
self.browser.driver.get(f'http://{self.address}/json')
super()._driver_init(tab_id)
self._driver.set_callback('Inspector.detached', self._onInspectorDetached)
self._driver.set_callback('Inspector.detached', self._onInspectorDetached, immediate=True)
def _reload(self):
"""重新获取document"""
@ -136,19 +136,23 @@ class ChromiumFrame(ChromiumBase):
self._is_diff_domain = True
if self._listener:
self._listener._to_target(node['frameId'], self.address, self)
super().__init__(self.address, node['frameId'], self._target_page.timeout)
end_time = perf_counter() + self.timeouts.page_load
while perf_counter() < end_time:
try:
obj_id = super().run_js('document;', as_expr=True)['objectId']
self.doc_ele = ChromiumElement(self, obj_id=obj_id)
break
except Exception as e:
sleep(.1)
if self._debug:
print(f'获取doc失败重试 {e}')
else:
raise GetDocumentError
super().__init__(self.address, node['frameId'], self._target_page.timeout)
timeout = end_time - perf_counter()
if timeout <= 0:
timeout = .5
self._wait_loaded(timeout)
# while perf_counter() < end_time:
# try:
# obj_id = super().run_js('document;', as_expr=True)['objectId']
# self.doc_ele = ChromiumElement(self, obj_id=obj_id)
# break
# except Exception as e:
# sleep(.1)
# if self._debug:
# print(f'获取doc失败重试 {e}')
# else:
# raise GetDocumentError
self._debug = debug
self.driver._debug = d_debug
@ -201,26 +205,14 @@ class ChromiumFrame(ChromiumBase):
def _onInspectorDetached(self, **kwargs):
"""异域转同域或退出"""
if self._debug:
print(f'{self._frame_id}触发InspectorDetached')
self._reload()
if self._debug:
print(f'{self._frame_id}执行InspectorDetached完毕')
def _onFrameDetached(self, **kwargs):
"""同域变异域"""
self.browser._frames.pop(kwargs['frameId'], None)
if kwargs['frameId'] == self._frame_id:
if self._debug:
print(f'{self._frame_id}触发FrameDetached')
self._reload()
if self._debug:
print(f'{self._frame_id}执行FrameDetached完毕')
# ----------挂件----------
@property

View File

@ -252,7 +252,7 @@ class SelectElement(object):
"""
if isinstance(option, (list, tuple, set)):
if not self.is_multi and len(option) > 1:
raise TypeError("只能对多项选框执行多选。")
option = option[:1]
for o in option:
o.run_js(f'this.selected={mode};')
self._dispatch_change()

View File

@ -1,7 +1,7 @@
requests
lxml
cssselect
DownloadKit>=2.0.0b2
DownloadKit>=2.0.0b3
websocket-client>=1.7.0
click
tldextract

View File

@ -6,7 +6,7 @@ with open("README.md", "r", encoding='utf-8') as fh:
setup(
name="DrissionPage",
version="4.0.0b29",
version="4.0.0b30",
author="g1879",
author_email="g1879@qq.com",
description="Python based web automation tool. It can control the browser and send and receive data packets.",
@ -22,19 +22,19 @@ setup(
'lxml',
'requests',
'cssselect',
'DownloadKit>=2.0.0b2',
'DownloadKit>=2.0.0b3',
'websocket-client>=1.7.0',
'click',
'tldextract',
'psutil'
],
classifiers=[
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.6",
"Development Status :: 4 - Beta",
"Topic :: Utilities",
"License :: OSI Approved :: BSD License",
],
python_requires='>=3.8',
python_requires='>=3.6',
entry_points={
'console_scripts': [
'dp = DrissionPage.commons.cli:main',