Skip to content

Commit b02553c

Browse files
[py] Add docstrings for RelativeBy
1 parent 6010b2c commit b02553c

File tree

1 file changed

+79
-8
lines changed

1 file changed

+79
-8
lines changed

py/selenium/webdriver/support/relative_locator.py

Lines changed: 79 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,59 +23,130 @@
2323

2424

2525
def with_tag_name(tag_name: str) -> "RelativeBy":
26+
"""
27+
Start searching for relative objects using a tag name.
28+
29+
Note: This method may be removed in future versions, please use
30+
`locate_with` instead.
31+
:Args:
32+
- tag_name: the DOM tag of element to start searching.
33+
:Returns:
34+
- RelativeBy - use this object to create filters within a
35+
`find_elements` call.
36+
"""
2637
if not tag_name:
2738
raise WebDriverException("tag_name can not be null")
2839
return RelativeBy({"css selector": tag_name})
2940

3041

3142
def locate_with(by: By, using: str) -> "RelativeBy":
43+
"""
44+
Start searching for relative objects your search criteria with By.
45+
46+
:Args:
47+
- by: The value from `By` passed in.
48+
- using: search term to find the element with.
49+
:Returns:
50+
- RelativeBy - use this object to create filters within a
51+
`find_elements` call.
52+
"""
3253
assert by is not None, "Please pass in a by argument"
3354
assert using is not None, "Please pass in a using argument"
3455
return RelativeBy({by: using})
3556

3657

3758
class RelativeBy(object):
38-
39-
def __init__(self, root: Dict = None, filters: List = None):
59+
"""
60+
Gives the opportunity to find elements based on their relative location
61+
on the page from a root elelemt. It is recommended that you use the helper
62+
function to create it.
63+
64+
Example:
65+
lowest = driver.find_element(By.ID, "below")
66+
67+
elements = driver.find_elements(locate_with(By.CSS_SELECTOR, "p").above(lowest))
68+
69+
ids = [el.get_attribute('id') for el in elements]
70+
assert "above" in ids
71+
assert "mid" in ids
72+
"""
73+
74+
def __init__(self, root: Dict[By, str] = None, filters: List = None):
75+
"""
76+
Creates a new RelativeBy object. It is prefered if you use the
77+
`locate_with` method as this signature could change.
78+
:Args:
79+
root - A dict with `By` enum as the key and the search query as the value
80+
filters - A list of the filters that will be searched. If none are passed
81+
in please use the fluent API on the object to create the filters
82+
"""
4083
self.root = root
4184
self.filters = filters or []
4285

43-
def above(self, element_or_locator: Union[WebElement, Dict] = None):
86+
def above(self, element_or_locator: Union[WebElement, Dict] = None) -> "RelativeBy":
87+
"""
88+
Add a filter to look for elements above.
89+
:Args:
90+
- element_or_locator: Element to look above
91+
"""
4492
if not element_or_locator:
4593
raise WebDriverException("Element or locator must be given when calling above method")
4694

4795
self.filters.append({"kind": "above", "args": [element_or_locator]})
4896
return self
4997

50-
def below(self, element_or_locator: Union[WebElement, Dict] = None):
98+
def below(self, element_or_locator: Union[WebElement, Dict] = None) -> "RelativeBy":
99+
"""
100+
Add a filter to look for elements below.
101+
:Args:
102+
- element_or_locator: Element to look below
103+
"""
51104
if not element_or_locator:
52105
raise WebDriverException("Element or locator must be given when calling above method")
53106

54107
self.filters.append({"kind": "below", "args": [element_or_locator]})
55108
return self
56109

57-
def to_left_of(self, element_or_locator: Union[WebElement, Dict] = None):
110+
def to_left_of(self, element_or_locator: Union[WebElement, Dict] = None) -> "RelativeBy":
111+
"""
112+
Add a filter to look for elements to the left of.
113+
:Args:
114+
- element_or_locator: Element to look to the left of
115+
"""
58116
if not element_or_locator:
59117
raise WebDriverException("Element or locator must be given when calling above method")
60118

61119
self.filters.append({"kind": "left", "args": [element_or_locator]})
62120
return self
63121

64-
def to_right_of(self, element_or_locator: Union[WebElement, Dict] = None):
122+
def to_right_of(self, element_or_locator: Union[WebElement, Dict] = None) -> "RelativeBy":
123+
"""
124+
Add a filter to look for elements right of.
125+
:Args:
126+
- element_or_locator: Element to look right of
127+
"""
65128
if not element_or_locator:
66129
raise WebDriverException("Element or locator must be given when calling above method")
67130

68131
self.filters.append({"kind": "right", "args": [element_or_locator]})
69132
return self
70133

71-
def near(self, element_or_locator_distance: Union[WebElement, Dict, int] = None):
134+
def near(self, element_or_locator_distance: Union[WebElement, Dict, int] = None) -> "RelativeBy":
135+
"""
136+
Add a filter to look for elements above.
137+
:Args:
138+
- element_or_locator_distance: Element to look near by the element or within a distance
139+
"""
72140
if not element_or_locator_distance:
73141
raise WebDriverException("Element or locator or distance must be given when calling above method")
74142

75143
self.filters.append({"kind": "near", "args": [element_or_locator_distance]})
76144
return self
77145

78-
def to_dict(self):
146+
def to_dict(self) -> Dict:
147+
"""
148+
Create a dict that will be passed to the driver to start searching for the element
149+
"""
79150
return {
80151
'relative': {
81152
'root': self.root,

0 commit comments

Comments
 (0)