27
27
from SeleniumLibrary .utils import escape_xpath_value , events , is_falsy
28
28
29
29
from .customlocator import CustomLocator
30
+ from .utilities import is_webelement , disallow_webelement_parent
30
31
31
-
32
- class Finder ():
32
+ class ElementFinder (Protocol ):
33
33
def __int__ (self ):
34
34
"""Placeholder to Finder class instantiation method """
35
- pass
35
+ ...
36
36
37
37
def pre_find_action (self ):
38
38
"""Placeholder for the pre_find_action method"""
39
- pass
39
+ ...
40
40
41
41
def find (self ):
42
42
"""Placeholder for the find method"""
43
- pass
44
-
45
- def _is_webelement (self , element ):
46
- # Hook for unit tests
47
- return isinstance (element , (WebElement , EventFiringWebElement ))
48
-
49
-
50
- def _parse_locator (self , locator ):
51
- if re .match (r"\(*//" , locator ):
52
- return "xpath" , locator
53
- index = self ._get_locator_separator_index (locator )
54
- if index != - 1 :
55
- prefix = locator [:index ].strip ()
56
- if prefix in self ._strategies :
57
- return prefix , locator [index + 1 :].lstrip ()
58
- return "default" , locator
43
+ ...
59
44
60
45
61
46
class FinderList ():
@@ -68,14 +53,17 @@ def __getitem__(self, item):
68
53
def __len__ (self ):
69
54
pass
70
55
71
- class DefaultFinder (Finder ):
56
+ class DefaultElementFinder :
57
+ def pre_find_action (self ):
58
+ pass
59
+
72
60
def find (self , locator , tag = None , first_only = True , required = True , parent = None ):
73
61
element_type = "Element" if not tag else tag .capitalize ()
74
- if parent and not self . _is_webelement (parent ):
62
+ if parent and not is_webelement (parent ):
75
63
raise ValueError (
76
64
f"Parent must be Selenium WebElement but it was { type (parent )} ."
77
65
)
78
- if self . _is_webelement (locator ):
66
+ if is_webelement (locator ):
79
67
return locator
80
68
prefix , criteria = self ._parse_locator (locator )
81
69
strategy = self ._strategies [prefix ]
@@ -133,14 +121,6 @@ def _get_tag_and_constraints(self, tag):
133
121
tag = "textarea"
134
122
return tag , constraints
135
123
136
- def _is_webelement (self , element ):
137
- # Hook for unit tests
138
- return isinstance (element , (WebElement , EventFiringWebElement ))
139
-
140
- def _disallow_webelement_parent (self , element ):
141
- if _is_webelement (element ):
142
- raise ValueError ("This method does not allow WebElement as parent" )
143
-
144
124
145
125
class LocatorElementEngine (ContextAware ):
146
126
def __init__ (self , ctx ):
@@ -186,11 +166,11 @@ def _split_locator(self, locator: Union[str, list]) -> list:
186
166
187
167
def _find (self , locator , tag = None , first_only = True , required = True , parent = None ):
188
168
element_type = "Element" if not tag else tag .capitalize ()
189
- if parent and not self . _is_webelement (parent ):
169
+ if parent and not is_webelement (parent ):
190
170
raise ValueError (
191
171
f"Parent must be Selenium WebElement but it was { type (parent )} ."
192
172
)
193
- if self . _is_webelement (locator ):
173
+ if is_webelement (locator ):
194
174
return locator
195
175
prefix , criteria = self ._parse_locator (locator )
196
176
strategy = self ._strategies [prefix ]
@@ -351,7 +331,7 @@ def _find_by_xpath(self, criteria, tag, constraints, parent):
351
331
)
352
332
353
333
def _find_by_dom (self , criteria , tag , constraints , parent ):
354
- self . _disallow_webelement_parent (parent )
334
+ disallow_webelement_parent (parent )
355
335
result = self .driver .execute_script (f"return { criteria } ;" )
356
336
if result is None :
357
337
return []
@@ -360,7 +340,7 @@ def _find_by_dom(self, criteria, tag, constraints, parent):
360
340
return self ._filter_elements (result , tag , constraints )
361
341
362
342
def _find_by_jquery_selector (self , criteria , tag , constraints , parent ):
363
- self . _disallow_webelement_parent (parent )
343
+ disallow_webelement_parent (parent )
364
344
criteria = criteria .replace ("'" , "\\ '" )
365
345
js = f"return jQuery('{ criteria } ').get();"
366
346
return self ._filter_elements (self .driver .execute_script (js ), tag , constraints )
@@ -404,7 +384,7 @@ def _find_by_data_locator(self, criteria, tag, constraints, parent):
404
384
return self ._find_by_xpath (local_criteria , tag , constraints , parent )
405
385
406
386
def _find_by_sc_locator (self , criteria , tag , constraints , parent ):
407
- self . _disallow_webelement_parent (parent )
387
+ disallow_webelement_parent (parent )
408
388
criteria = criteria .replace ("'" , "\\ '" )
409
389
js = f"return isc.AutoTest.getElement('{ criteria } ')"
410
390
return self ._filter_elements ([self .driver .execute_script (js )], tag , constraints )
0 commit comments