Skip to content

Commit 7719d5f

Browse files
authored
[py] Added Docstrings to RelativeBy Class and Added Missing Deprecation Warning to with_tag_name() (SeleniumHQ#15097)
1 parent 11e1309 commit 7719d5f

File tree

1 file changed

+147
-35
lines changed

1 file changed

+147
-35
lines changed

py/selenium/webdriver/support/relative_locator.py

+147-35
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
# KIND, either express or implied. See the License for the
1515
# specific language governing permissions and limitations
1616
# under the License.
17+
import warnings
1718
from typing import Dict
1819
from typing import List
1920
from typing import NoReturn
@@ -30,15 +31,29 @@
3031
def with_tag_name(tag_name: str) -> "RelativeBy":
3132
"""Start searching for relative objects using a tag name.
3233
33-
Note: This method may be removed in future versions, please use
34-
`locate_with` instead.
35-
36-
:Args:
37-
- tag_name: the DOM tag of element to start searching.
38-
:Returns:
39-
- RelativeBy: use this object to create filters within a
40-
`find_elements` call.
34+
Parameters:
35+
----------
36+
tag_name : str
37+
The DOM tag of element to start searching.
38+
39+
Returns:
40+
--------
41+
RelativeBy
42+
Use this object to create filters within a `find_elements` call.
43+
44+
Raises:
45+
-------
46+
WebDriverException
47+
If `tag_name` is None.
48+
49+
Notes:
50+
------
51+
- This method is deprecated and may be removed in future versions.
52+
- Please use `locate_with` instead.
4153
"""
54+
warnings.warn(
55+
"This method is deprecated and may be removed in future versions. " "Please use `locate_with` instead."
56+
)
4257
if not tag_name:
4358
raise WebDriverException("tag_name can not be null")
4459
return RelativeBy({By.CSS_SELECTOR: tag_name})
@@ -47,12 +62,23 @@ def with_tag_name(tag_name: str) -> "RelativeBy":
4762
def locate_with(by: ByType, using: str) -> "RelativeBy":
4863
"""Start searching for relative objects your search criteria with By.
4964
50-
:Args:
51-
- by: The value from `By` passed in.
52-
- using: search term to find the element with.
53-
:Returns:
54-
- RelativeBy: use this object to create filters within a
55-
`find_elements` call.
65+
Parameters:
66+
----------
67+
by : ByType
68+
The method to find the element.
69+
70+
using : str
71+
The value from `By` passed in.
72+
73+
Returns:
74+
--------
75+
RelativeBy
76+
Use this object to create filters within a `find_elements` call.
77+
78+
Example:
79+
--------
80+
>>> lowest = driver.find_element(By.ID, "below")
81+
>>> elements = driver.find_elements(locate_with(By.CSS_SELECTOR, "p").above(lowest))
5682
"""
5783
assert by is not None, "Please pass in a by argument"
5884
assert using is not None, "Please pass in a using argument"
@@ -65,13 +91,12 @@ class RelativeBy:
6591
function to create it.
6692
6793
Example:
68-
lowest = driver.find_element(By.ID, "below")
69-
70-
elements = driver.find_elements(locate_with(By.CSS_SELECTOR, "p").above(lowest))
71-
72-
ids = [el.get_attribute('id') for el in elements]
73-
assert "above" in ids
74-
assert "mid" in ids
94+
--------
95+
>>> lowest = driver.find_element(By.ID, "below")
96+
>>> elements = driver.find_elements(locate_with(By.CSS_SELECTOR, "p").above(lowest))
97+
>>> ids = [el.get_attribute('id') for el in elements]
98+
>>> assert "above" in ids
99+
>>> assert "mid" in ids
75100
"""
76101

77102
LocatorType = Dict[ByType, str]
@@ -80,9 +105,13 @@ def __init__(self, root: Optional[Dict[ByType, str]] = None, filters: Optional[L
80105
"""Creates a new RelativeBy object. It is preferred if you use the
81106
`locate_with` method as this signature could change.
82107
83-
:Args:
84-
root - A dict with `By` enum as the key and the search query as the value
85-
filters - A list of the filters that will be searched. If none are passed
108+
Attributes:
109+
----------
110+
root : Dict[By, str]
111+
- A dict with `By` enum as the key and the search query as the value
112+
113+
filters : List
114+
- A list of the filters that will be searched. If none are passed
86115
in please use the fluent API on the object to create the filters
87116
"""
88117
self.root = root
@@ -97,8 +126,24 @@ def above(self, element_or_locator: None = None) -> "NoReturn": ...
97126
def above(self, element_or_locator: Union[WebElement, LocatorType, None] = None) -> "RelativeBy":
98127
"""Add a filter to look for elements above.
99128
100-
:Args:
101-
- element_or_locator: Element to look above
129+
Parameters:
130+
----------
131+
element_or_locator : Union[WebElement, Dict, None]
132+
Element to look above
133+
134+
Returns:
135+
--------
136+
RelativeBy
137+
138+
Raises:
139+
-------
140+
WebDriverException
141+
If `element_or_locator` is None.
142+
143+
Example:
144+
--------
145+
>>> lowest = driver.find_element(By.ID, "below")
146+
>>> elements = driver.find_elements(locate_with(By.CSS_SELECTOR, "p").above(lowest))
102147
"""
103148
if not element_or_locator:
104149
raise WebDriverException("Element or locator must be given when calling above method")
@@ -115,8 +160,24 @@ def below(self, element_or_locator: None = None) -> "NoReturn": ...
115160
def below(self, element_or_locator: Union[WebElement, Dict, None] = None) -> "RelativeBy":
116161
"""Add a filter to look for elements below.
117162
118-
:Args:
119-
- element_or_locator: Element to look below
163+
Parameters:
164+
----------
165+
element_or_locator : Union[WebElement, Dict, None]
166+
Element to look below
167+
168+
Returns:
169+
--------
170+
RelativeBy
171+
172+
Raises:
173+
-------
174+
WebDriverException
175+
If `element_or_locator` is None.
176+
177+
Example:
178+
--------
179+
>>> highest = driver.find_element(By.ID, "high")
180+
>>> elements = driver.find_elements(locate_with(By.CSS_SELECTOR, "p").below(highest))
120181
"""
121182
if not element_or_locator:
122183
raise WebDriverException("Element or locator must be given when calling below method")
@@ -133,8 +194,24 @@ def to_left_of(self, element_or_locator: None = None) -> "NoReturn": ...
133194
def to_left_of(self, element_or_locator: Union[WebElement, Dict, None] = None) -> "RelativeBy":
134195
"""Add a filter to look for elements to the left of.
135196
136-
:Args:
137-
- element_or_locator: Element to look to the left of
197+
Parameters:
198+
----------
199+
element_or_locator : Union[WebElement, Dict, None]
200+
Element to look to the left of
201+
202+
Returns:
203+
--------
204+
RelativeBy
205+
206+
Raises:
207+
-------
208+
WebDriverException
209+
If `element_or_locator` is None.
210+
211+
Example:
212+
--------
213+
>>> right = driver.find_element(By.ID, "right")
214+
>>> elements = driver.find_elements(locate_with(By.CSS_SELECTOR, "p").to_left_of(right))
138215
"""
139216
if not element_or_locator:
140217
raise WebDriverException("Element or locator must be given when calling to_left_of method")
@@ -151,8 +228,24 @@ def to_right_of(self, element_or_locator: None = None) -> "NoReturn": ...
151228
def to_right_of(self, element_or_locator: Union[WebElement, Dict, None] = None) -> "RelativeBy":
152229
"""Add a filter to look for elements right of.
153230
154-
:Args:
155-
- element_or_locator: Element to look right of
231+
Parameters:
232+
----------
233+
element_or_locator : Union[WebElement, Dict, None]
234+
Element to look right of
235+
236+
Returns:
237+
--------
238+
RelativeBy
239+
240+
Raises:
241+
-------
242+
WebDriverException
243+
If `element_or_locator` is None.
244+
245+
Example:
246+
--------
247+
>>> left = driver.find_element(By.ID, "left")
248+
>>> elements = driver.find_elements(locate_with(By.CSS_SELECTOR, "p").to_right_of(left))
156249
"""
157250
if not element_or_locator:
158251
raise WebDriverException("Element or locator must be given when calling to_right_of method")
@@ -241,9 +334,28 @@ def near(self, element_or_locator: None = None, distance: int = 50) -> "NoReturn
241334
def near(self, element_or_locator: Union[WebElement, LocatorType, None] = None, distance: int = 50) -> "RelativeBy":
242335
"""Add a filter to look for elements near.
243336
244-
:Args:
245-
- element_or_locator: Element to look near by the element or within a distance
246-
- distance: distance in pixel
337+
Parameters:
338+
----------
339+
element_or_locator : Union[WebElement, Dict, None]
340+
Element to look near by the element or within a distance
341+
342+
distance : int
343+
Distance in pixel
344+
345+
Returns:
346+
--------
347+
RelativeBy
348+
349+
Raises:
350+
-------
351+
WebDriverException
352+
- If `element_or_locator` is None
353+
- If `distance` is less than or equal to 0.
354+
355+
Example:
356+
--------
357+
>>> near = driver.find_element(By.ID, "near")
358+
>>> elements = driver.find_elements(locate_with(By.CSS_SELECTOR, "p").near(near, 50))
247359
"""
248360
if not element_or_locator:
249361
raise WebDriverException("Element or locator must be given when calling near method")

0 commit comments

Comments
 (0)