2
2
"""
3
3
@Author : g1879
4
4
5
+ @Website : https://DrissionPage.cn
5
6
@Copyright: (c) 2020 by g1879, Inc. All Rights Reserved.
6
7
"""
7
8
from abc import abstractmethod
16
17
from .._configs .session_options import SessionOptions
17
18
from .._elements .none_element import NoneElement
18
19
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
21
22
from .._functions .web import format_html
22
- from ..errors import ElementNotFoundError
23
+ from ..errors import ElementNotFoundError , LocatorError
23
24
24
25
25
26
class BaseParser (object ):
26
- """所有页面、元素类的基类"""
27
-
28
27
def __call__ (self , locator ):
29
28
return self .ele (locator )
30
29
@@ -39,10 +38,6 @@ def find(self, locators, any_one=True, first_ele=True, timeout=None):
39
38
timeout = 0
40
39
if timeout is None :
41
40
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 } " )
46
41
r = get_eles (locators , self , any_one , first_ele , timeout )
47
42
if any_one :
48
43
for ele in r :
@@ -70,15 +65,14 @@ def _find_elements(self, locator, timeout, index=1, relative=False, raise_err=No
70
65
71
66
72
67
class BaseElement (BaseParser ):
73
- """各元素类的基类"""
74
-
75
68
def __init__ (self , owner = None ):
76
69
self .owner = owner
77
70
self ._type = 'BaseElement'
78
71
79
72
def get_frame (self , loc_or_ind , timeout = None ):
80
73
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 ))
82
76
return get_frame (self , loc_ind_ele = loc_or_ind , timeout = timeout )
83
77
84
78
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
89
83
r = self ._find_elements (locator , timeout = timeout , index = index , relative = relative , raise_err = raise_err )
90
84
if r or isinstance (r , list ):
91
85
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 })
94
88
95
89
r .method = method
96
90
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
100
94
def timeout (self ):
101
95
return self .owner .timeout if self .owner else 10
102
96
97
+ @property
98
+ def child_count (self ):
99
+ return int (self ._ele ('xpath:count(./*)' ))
100
+
103
101
# ----------------以下属性或方法由后代实现----------------
104
102
@property
105
103
def tag (self ):
@@ -145,11 +143,12 @@ def parent(self, level_or_loc=1, index=1, timeout=None):
145
143
elif isinstance (level_or_loc , (tuple , str )):
146
144
loc = get_loc (level_or_loc , True )
147
145
if loc [0 ] == 'css selector' :
148
- raise ValueError ( '此css selector语法不受支持,请换成xpath。' )
146
+ raise LocatorError ( _S . _lang . UNSUPPORTED_CSS_SYNTAX )
149
147
loc = f'xpath:./ancestor::{ loc [1 ].lstrip (". / " )} [{ index } ]'
150
148
151
149
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 ))
153
152
154
153
return self ._ele (loc , timeout = timeout , relative = True , raise_err = False , method = 'parent()' )
155
154
@@ -162,7 +161,7 @@ def child(self, locator='', index=1, timeout=None, ele_only=True):
162
161
else :
163
162
loc = get_loc (locator , True ) # 把定位符转换为xpath
164
163
if loc [0 ] == 'css selector' :
165
- raise ValueError ( '此css selector语法不受支持,请换成xpath。' )
164
+ raise LocatorError ( _S . _lang . UNSUPPORTED_CSS_SYNTAX )
166
165
loc = loc [1 ].lstrip ('./' )
167
166
168
167
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):
187
186
else :
188
187
loc = get_loc (locator , True ) # 把定位符转换为xpath
189
188
if loc [0 ] == 'css selector' :
190
- raise ValueError ( '此css selector语法不受支持,请换成xpath。' )
189
+ raise LocatorError ( _S . _lang . UNSUPPORTED_CSS_SYNTAX )
191
190
loc = loc [1 ].lstrip ('./' )
192
191
193
192
loc = f'xpath:./{ loc } '
@@ -225,7 +224,7 @@ def _get_relatives(self, index=None, locator='', direction='following', brother=
225
224
else :
226
225
loc = get_loc (locator , True ) # 把定位符转换为xpath
227
226
if loc [0 ] == 'css selector' :
228
- raise ValueError ( '此css selector语法不受支持,请换成xpath。' )
227
+ raise LocatorError ( _S . _lang . UNSUPPORTED_CSS_SYNTAX )
229
228
loc = loc [1 ].lstrip ('./' )
230
229
231
230
loc = f'xpath:./{ direction } { brother } ::{ loc } '
@@ -251,7 +250,7 @@ def raw_text(self):
251
250
return
252
251
253
252
@abstractmethod
254
- def attr (self , name : str ):
253
+ def attr (self , name ):
255
254
return ''
256
255
257
256
def _get_ele_path (self , xpath = True ):
@@ -348,15 +347,15 @@ def get(self, url, show_errmsg=False, retry=None, interval=None):
348
347
349
348
def _ele (self , locator , timeout = None , index = 1 , raise_err = None , method = None ):
350
349
if not locator :
351
- raise ElementNotFoundError (None , method , {'locator' : locator })
350
+ raise ElementNotFoundError (METHOD = method , ARGS = {'locator' : locator , 'index' : index , 'timeout' : timeout })
352
351
if timeout is None :
353
352
timeout = self .timeout
354
353
355
354
r = self ._find_elements (locator , timeout = timeout , index = index , raise_err = raise_err )
356
355
if r or isinstance (r , list ):
357
356
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 })
360
359
361
360
r .method = method
362
361
r .args = {'locator' : locator , 'index' : index , 'timeout' : timeout }
0 commit comments