Skip to content

Commit 7872398

Browse files
committed
4.1.0.15
1 parent 5e848dc commit 7872398

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

90 files changed

+1261
-517
lines changed

.idea/.gitignore

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/DrissionPage_github.iml

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/inspectionProfiles/Project_Default.xml

Lines changed: 70 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/inspectionProfiles/profiles_settings.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/modules.xml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/vcs.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

DrissionPage/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"""
33
@Author : g1879
44
5+
@Website : https://DrissionPage.cn
56
@Copyright: (c) 2020 by g1879, Inc. All Rights Reserved.
67
78
允许任何人以个人身份使用或分发本项目源代码,但仅限于学习和合法非盈利目的。
@@ -24,5 +25,4 @@
2425
from ._pages.chromium_page import ChromiumPage
2526
from ._pages.session_page import SessionPage
2627
from ._pages.web_page import WebPage
27-
28-
__version__ = '4.1.0.13'
28+
from .version import __version__

DrissionPage/__init__.pyi

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"""
33
@Author : g1879
44
5+
@Website : https://DrissionPage.cn
56
@Copyright: (c) 2020 by g1879, Inc. All Rights Reserved.
67
"""
78
from ._base.chromium import Chromium
@@ -10,6 +11,7 @@ from ._configs.session_options import SessionOptions
1011
from ._pages.chromium_page import ChromiumPage
1112
from ._pages.session_page import SessionPage
1213
from ._pages.web_page import WebPage
14+
from .version import __version__
15+
1316

1417
__all__ = ['WebPage', 'ChromiumPage', 'Chromium', 'ChromiumOptions', 'SessionOptions', 'SessionPage', '__version__']
15-
__version__: str = ...

DrissionPage/_base/base.py

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"""
33
@Author : g1879
44
5+
@Website : https://DrissionPage.cn
56
@Copyright: (c) 2020 by g1879, Inc. All Rights Reserved.
67
"""
78
from abc import abstractmethod
@@ -16,15 +17,13 @@
1617
from .._configs.session_options import SessionOptions
1718
from .._elements.none_element import NoneElement
1819
from .._functions.elements import get_frame, get_eles
19-
from .._functions.locator import get_loc, is_selenium_loc
20-
from .._functions.settings import Settings
20+
from .._functions.locator import get_loc
21+
from .._functions.settings import Settings as _S
2122
from .._functions.web import format_html
22-
from ..errors import ElementNotFoundError
23+
from ..errors import ElementNotFoundError, LocatorError
2324

2425

2526
class BaseParser(object):
26-
"""所有页面、元素类的基类"""
27-
2827
def __call__(self, locator):
2928
return self.ele(locator)
3029

@@ -39,10 +38,6 @@ def find(self, locators, any_one=True, first_ele=True, timeout=None):
3938
timeout = 0
4039
if timeout is None:
4140
timeout = self.timeout
42-
if isinstance(locators, tuple) and not is_selenium_loc(locators):
43-
raise ValueError(f"locators参数为tuple时必须是单独的定位符,即长度为2,且第一位是'id', 'xpath', 'link text', "
44-
f"'partial link text','name', 'tag name', 'class name', 'css selector' 之一。\n"
45-
f"现在是:{locators}")
4641
r = get_eles(locators, self, any_one, first_ele, timeout)
4742
if any_one:
4843
for ele in r:
@@ -70,15 +65,14 @@ def _find_elements(self, locator, timeout, index=1, relative=False, raise_err=No
7065

7166

7267
class BaseElement(BaseParser):
73-
"""各元素类的基类"""
74-
7568
def __init__(self, owner=None):
7669
self.owner = owner
7770
self._type = 'BaseElement'
7871

7972
def get_frame(self, loc_or_ind, timeout=None):
8073
if not isinstance(loc_or_ind, (int, str, tuple)):
81-
raise TypeError('loc_or_ind参数是定位符或序号。')
74+
raise ValueError(_S._lang.join(_S._lang.INCORRECT_TYPE_, 'loc_or_ind',
75+
ALLOW_TYPE=_S._lang.LOC_OR_IND, CURR_VAL=loc_or_ind))
8276
return get_frame(self, loc_ind_ele=loc_or_ind, timeout=timeout)
8377

8478
def _ele(self, locator, timeout=None, index=1, relative=False, raise_err=None, method=None):
@@ -89,8 +83,8 @@ def _ele(self, locator, timeout=None, index=1, relative=False, raise_err=None, m
8983
r = self._find_elements(locator, timeout=timeout, index=index, relative=relative, raise_err=raise_err)
9084
if r or isinstance(r, list):
9185
return r
92-
if raise_err is True or (Settings.raise_when_ele_not_found and raise_err is None):
93-
raise ElementNotFoundError(None, method, {'locator': locator, 'index': index, 'timeout': timeout})
86+
if raise_err is True or (_S.raise_when_ele_not_found and raise_err is None):
87+
raise ElementNotFoundError(METHOD=method, ARGS={'locator': locator, 'index': index, 'timeout': timeout})
9488

9589
r.method = method
9690
r.args = {'locator': locator, 'index': index, 'timeout': timeout}
@@ -100,6 +94,10 @@ def _ele(self, locator, timeout=None, index=1, relative=False, raise_err=None, m
10094
def timeout(self):
10195
return self.owner.timeout if self.owner else 10
10296

97+
@property
98+
def child_count(self):
99+
return int(self._ele('xpath:count(./*)'))
100+
103101
# ----------------以下属性或方法由后代实现----------------
104102
@property
105103
def tag(self):
@@ -145,11 +143,12 @@ def parent(self, level_or_loc=1, index=1, timeout=None):
145143
elif isinstance(level_or_loc, (tuple, str)):
146144
loc = get_loc(level_or_loc, True)
147145
if loc[0] == 'css selector':
148-
raise ValueError('此css selector语法不受支持,请换成xpath。')
146+
raise LocatorError(_S._lang.UNSUPPORTED_CSS_SYNTAX)
149147
loc = f'xpath:./ancestor::{loc[1].lstrip(". / ")}[{index}]'
150148

151149
else:
152-
raise TypeError('level_or_loc参数只能是tuple、int或str。')
150+
raise ValueError(_S._lang.join(_S._lang.INCORRECT_TYPE_, 'level_or_loc', ALLOW_TYPE='tuple, int, str',
151+
CURR_VAL=level_or_loc))
153152

154153
return self._ele(loc, timeout=timeout, relative=True, raise_err=False, method='parent()')
155154

@@ -162,7 +161,7 @@ def child(self, locator='', index=1, timeout=None, ele_only=True):
162161
else:
163162
loc = get_loc(locator, True) # 把定位符转换为xpath
164163
if loc[0] == 'css selector':
165-
raise ValueError('此css selector语法不受支持,请换成xpath。')
164+
raise LocatorError(_S._lang.UNSUPPORTED_CSS_SYNTAX)
166165
loc = loc[1].lstrip('./')
167166

168167
node = self._ele(f'xpath:./{loc}', timeout=timeout, index=index, relative=True, raise_err=False)
@@ -187,7 +186,7 @@ def children(self, locator='', timeout=None, ele_only=True):
187186
else:
188187
loc = get_loc(locator, True) # 把定位符转换为xpath
189188
if loc[0] == 'css selector':
190-
raise ValueError('此css selector语法不受支持,请换成xpath。')
189+
raise LocatorError(_S._lang.UNSUPPORTED_CSS_SYNTAX)
191190
loc = loc[1].lstrip('./')
192191

193192
loc = f'xpath:./{loc}'
@@ -225,7 +224,7 @@ def _get_relatives(self, index=None, locator='', direction='following', brother=
225224
else:
226225
loc = get_loc(locator, True) # 把定位符转换为xpath
227226
if loc[0] == 'css selector':
228-
raise ValueError('此css selector语法不受支持,请换成xpath。')
227+
raise LocatorError(_S._lang.UNSUPPORTED_CSS_SYNTAX)
229228
loc = loc[1].lstrip('./')
230229

231230
loc = f'xpath:./{direction}{brother}::{loc}'
@@ -251,7 +250,7 @@ def raw_text(self):
251250
return
252251

253252
@abstractmethod
254-
def attr(self, name: str):
253+
def attr(self, name):
255254
return ''
256255

257256
def _get_ele_path(self, xpath=True):
@@ -348,15 +347,15 @@ def get(self, url, show_errmsg=False, retry=None, interval=None):
348347

349348
def _ele(self, locator, timeout=None, index=1, raise_err=None, method=None):
350349
if not locator:
351-
raise ElementNotFoundError(None, method, {'locator': locator})
350+
raise ElementNotFoundError(METHOD=method, ARGS={'locator': locator, 'index': index, 'timeout': timeout})
352351
if timeout is None:
353352
timeout = self.timeout
354353

355354
r = self._find_elements(locator, timeout=timeout, index=index, raise_err=raise_err)
356355
if r or isinstance(r, list):
357356
return r
358-
if raise_err is True or (Settings.raise_when_ele_not_found and raise_err is None):
359-
raise ElementNotFoundError(None, method, {'locator': locator, 'index': index, 'timeout': timeout})
357+
if raise_err is True or (_S.raise_when_ele_not_found and raise_err is None):
358+
raise ElementNotFoundError(METHOD=method, ARGS={'locator': locator, 'index': index, 'timeout': timeout})
360359

361360
r.method = method
362361
r.args = {'locator': locator, 'index': index, 'timeout': timeout}

DrissionPage/_base/base.pyi

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"""
33
@Author : g1879
44
5+
@Website : https://DrissionPage.cn
56
@Copyright: (c) 2020 by g1879, Inc. All Rights Reserved.
67
"""
78
from abc import abstractmethod
@@ -23,6 +24,7 @@ from .._pages.web_page import WebPage
2324

2425

2526
class BaseParser(object):
27+
"""所有页面、元素类的基类"""
2628
_type: str
2729
timeout: float
2830

@@ -78,6 +80,7 @@ class BaseParser(object):
7880

7981

8082
class BaseElement(BaseParser):
83+
"""各元素类的基类"""
8184
owner: BasePage = ...
8285

8386
def __init__(self, owner: BasePage = None): ...
@@ -87,6 +90,11 @@ class BaseElement(BaseParser):
8790
"""返回其查找元素时超时时间"""
8891
...
8992

93+
@property
94+
def child_count(self) -> int:
95+
"""返回直接子元素的个数"""
96+
...
97+
9098
# ----------------以下属性或方法由后代实现----------------
9199
@property
92100
def tag(self) -> str: ...

0 commit comments

Comments
 (0)