1616# under the License.
1717from typing import Dict
1818from typing import List
19+ from typing import NoReturn
1920from typing import Optional
2021from typing import Union
22+ from typing import overload
2123
2224from selenium .common .exceptions import WebDriverException
2325from selenium .webdriver .common .by import By
26+ from selenium .webdriver .common .by import ByType
2427from selenium .webdriver .remote .webelement import WebElement
2528
2629
@@ -37,10 +40,10 @@ def with_tag_name(tag_name: str) -> "RelativeBy":
3740 """
3841 if not tag_name :
3942 raise WebDriverException ("tag_name can not be null" )
40- return RelativeBy ({"css selector" : tag_name })
43+ return RelativeBy ({By . CSS_SELECTOR : tag_name })
4144
4245
43- def locate_with (by : By , using : str ) -> "RelativeBy" :
46+ def locate_with (by : ByType , using : str ) -> "RelativeBy" :
4447 """Start searching for relative objects your search criteria with By.
4548
4649 :Args:
@@ -70,7 +73,9 @@ class RelativeBy:
7073 assert "mid" in ids
7174 """
7275
73- def __init__ (self , root : Optional [Dict [Union [By , str ], str ]] = None , filters : Optional [List ] = None ):
76+ LocatorType = Dict [ByType , str ]
77+
78+ def __init__ (self , root : Optional [Dict [ByType , str ]] = None , filters : Optional [List ] = None ):
7479 """Creates a new RelativeBy object. It is preferred if you use the
7580 `locate_with` method as this signature could change.
7681
@@ -82,7 +87,15 @@ def __init__(self, root: Optional[Dict[Union[By, str], str]] = None, filters: Op
8287 self .root = root
8388 self .filters = filters or []
8489
85- def above (self , element_or_locator : Union [WebElement , Dict ] = None ) -> "RelativeBy" :
90+ @overload
91+ def above (self , element_or_locator : Union [WebElement , LocatorType ]) -> "RelativeBy" :
92+ ...
93+
94+ @overload
95+ def above (self , element_or_locator : None = None ) -> "NoReturn" :
96+ ...
97+
98+ def above (self , element_or_locator : Union [WebElement , LocatorType , None ] = None ) -> "RelativeBy" :
8699 """Add a filter to look for elements above.
87100
88101 :Args:
@@ -94,7 +107,15 @@ def above(self, element_or_locator: Union[WebElement, Dict] = None) -> "Relative
94107 self .filters .append ({"kind" : "above" , "args" : [element_or_locator ]})
95108 return self
96109
97- def below (self , element_or_locator : Union [WebElement , Dict ] = None ) -> "RelativeBy" :
110+ @overload
111+ def below (self , element_or_locator : Union [WebElement , LocatorType ]) -> "RelativeBy" :
112+ ...
113+
114+ @overload
115+ def below (self , element_or_locator : None = None ) -> "NoReturn" :
116+ ...
117+
118+ def below (self , element_or_locator : Union [WebElement , Dict , None ] = None ) -> "RelativeBy" :
98119 """Add a filter to look for elements below.
99120
100121 :Args:
@@ -106,7 +127,15 @@ def below(self, element_or_locator: Union[WebElement, Dict] = None) -> "Relative
106127 self .filters .append ({"kind" : "below" , "args" : [element_or_locator ]})
107128 return self
108129
109- def to_left_of (self , element_or_locator : Union [WebElement , Dict ] = None ) -> "RelativeBy" :
130+ @overload
131+ def to_left_of (self , element_or_locator : Union [WebElement , LocatorType ]) -> "RelativeBy" :
132+ ...
133+
134+ @overload
135+ def to_left_of (self , element_or_locator : None = None ) -> "NoReturn" :
136+ ...
137+
138+ def to_left_of (self , element_or_locator : Union [WebElement , Dict , None ] = None ) -> "RelativeBy" :
110139 """Add a filter to look for elements to the left of.
111140
112141 :Args:
@@ -118,7 +147,15 @@ def to_left_of(self, element_or_locator: Union[WebElement, Dict] = None) -> "Rel
118147 self .filters .append ({"kind" : "left" , "args" : [element_or_locator ]})
119148 return self
120149
121- def to_right_of (self , element_or_locator : Union [WebElement , Dict ] = None ) -> "RelativeBy" :
150+ @overload
151+ def to_right_of (self , element_or_locator : Union [WebElement , LocatorType ]) -> "RelativeBy" :
152+ ...
153+
154+ @overload
155+ def to_right_of (self , element_or_locator : None = None ) -> "NoReturn" :
156+ ...
157+
158+ def to_right_of (self , element_or_locator : Union [WebElement , Dict , None ] = None ) -> "RelativeBy" :
122159 """Add a filter to look for elements right of.
123160
124161 :Args:
@@ -130,16 +167,27 @@ def to_right_of(self, element_or_locator: Union[WebElement, Dict] = None) -> "Re
130167 self .filters .append ({"kind" : "right" , "args" : [element_or_locator ]})
131168 return self
132169
133- def near (self , element_or_locator_distance : Union [WebElement , Dict , int ] = None ) -> "RelativeBy" :
170+ @overload
171+ def near (self , element_or_locator : Union [WebElement , LocatorType ], distance : int = 50 ) -> "RelativeBy" :
172+ ...
173+
174+ @overload
175+ def near (self , element_or_locator : None = None , distance : int = 50 ) -> "NoReturn" :
176+ ...
177+
178+ def near (self , element_or_locator : Union [WebElement , LocatorType , None ] = None , distance : int = 50 ) -> "RelativeBy" :
134179 """Add a filter to look for elements near.
135180
136181 :Args:
137- - element_or_locator_distance: Element to look near by the element or within a distance
182+ - element_or_locator: Element to look near by the element or within a distance
183+ - distance: distance in pixel
138184 """
139- if not element_or_locator_distance :
140- raise WebDriverException ("Element or locator or distance must be given when calling near method" )
185+ if not element_or_locator :
186+ raise WebDriverException ("Element or locator must be given when calling near method" )
187+ if distance <= 0 :
188+ raise WebDriverException ("Distance must be positive" )
141189
142- self .filters .append ({"kind" : "near" , "args" : [element_or_locator_distance ]})
190+ self .filters .append ({"kind" : "near" , "args" : [element_or_locator , distance ]})
143191 return self
144192
145193 def to_dict (self ) -> Dict :
0 commit comments