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,13 @@ 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+ @overload
94+ def above (self , element_or_locator : None = None ) -> "NoReturn" : ...
95+
96+ def above (self , element_or_locator : Union [WebElement , LocatorType , None ] = None ) -> "RelativeBy" :
8697 """Add a filter to look for elements above.
8798
8899 :Args:
@@ -94,7 +105,13 @@ def above(self, element_or_locator: Union[WebElement, Dict] = None) -> "Relative
94105 self .filters .append ({"kind" : "above" , "args" : [element_or_locator ]})
95106 return self
96107
97- def below (self , element_or_locator : Union [WebElement , Dict ] = None ) -> "RelativeBy" :
108+ @overload
109+ def below (self , element_or_locator : Union [WebElement , LocatorType ]) -> "RelativeBy" : ...
110+
111+ @overload
112+ def below (self , element_or_locator : None = None ) -> "NoReturn" : ...
113+
114+ def below (self , element_or_locator : Union [WebElement , Dict , None ] = None ) -> "RelativeBy" :
98115 """Add a filter to look for elements below.
99116
100117 :Args:
@@ -106,7 +123,13 @@ def below(self, element_or_locator: Union[WebElement, Dict] = None) -> "Relative
106123 self .filters .append ({"kind" : "below" , "args" : [element_or_locator ]})
107124 return self
108125
109- def to_left_of (self , element_or_locator : Union [WebElement , Dict ] = None ) -> "RelativeBy" :
126+ @overload
127+ def to_left_of (self , element_or_locator : Union [WebElement , LocatorType ]) -> "RelativeBy" : ...
128+
129+ @overload
130+ def to_left_of (self , element_or_locator : None = None ) -> "NoReturn" : ...
131+
132+ def to_left_of (self , element_or_locator : Union [WebElement , Dict , None ] = None ) -> "RelativeBy" :
110133 """Add a filter to look for elements to the left of.
111134
112135 :Args:
@@ -118,7 +141,13 @@ def to_left_of(self, element_or_locator: Union[WebElement, Dict] = None) -> "Rel
118141 self .filters .append ({"kind" : "left" , "args" : [element_or_locator ]})
119142 return self
120143
121- def to_right_of (self , element_or_locator : Union [WebElement , Dict ] = None ) -> "RelativeBy" :
144+ @overload
145+ def to_right_of (self , element_or_locator : Union [WebElement , LocatorType ]) -> "RelativeBy" : ...
146+
147+ @overload
148+ def to_right_of (self , element_or_locator : None = None ) -> "NoReturn" : ...
149+
150+ def to_right_of (self , element_or_locator : Union [WebElement , Dict , None ] = None ) -> "RelativeBy" :
122151 """Add a filter to look for elements right of.
123152
124153 :Args:
@@ -130,16 +159,25 @@ def to_right_of(self, element_or_locator: Union[WebElement, Dict] = None) -> "Re
130159 self .filters .append ({"kind" : "right" , "args" : [element_or_locator ]})
131160 return self
132161
133- def near (self , element_or_locator_distance : Union [WebElement , Dict , int ] = None ) -> "RelativeBy" :
162+ @overload
163+ def near (self , element_or_locator : Union [WebElement , LocatorType ], distance : int = 50 ) -> "RelativeBy" : ...
164+
165+ @overload
166+ def near (self , element_or_locator : None = None , distance : int = 50 ) -> "NoReturn" : ...
167+
168+ def near (self , element_or_locator : Union [WebElement , LocatorType , None ] = None , distance : int = 50 ) -> "RelativeBy" :
134169 """Add a filter to look for elements near.
135170
136171 :Args:
137- - element_or_locator_distance: Element to look near by the element or within a distance
172+ - element_or_locator: Element to look near by the element or within a distance
173+ - distance: distance in pixel
138174 """
139- if not element_or_locator_distance :
140- raise WebDriverException ("Element or locator or distance must be given when calling near method" )
175+ if not element_or_locator :
176+ raise WebDriverException ("Element or locator must be given when calling near method" )
177+ if distance <= 0 :
178+ raise WebDriverException ("Distance must be positive" )
141179
142- self .filters .append ({"kind" : "near" , "args" : [element_or_locator_distance ]})
180+ self .filters .append ({"kind" : "near" , "args" : [element_or_locator , distance ]})
143181 return self
144182
145183 def to_dict (self ) -> Dict :
0 commit comments