|
23 | 23 |
|
24 | 24 |
|
25 | 25 | 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 | + """ |
26 | 37 | if not tag_name:
|
27 | 38 | raise WebDriverException("tag_name can not be null")
|
28 | 39 | return RelativeBy({"css selector": tag_name})
|
29 | 40 |
|
30 | 41 |
|
31 | 42 | 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 | + """ |
32 | 53 | assert by is not None, "Please pass in a by argument"
|
33 | 54 | assert using is not None, "Please pass in a using argument"
|
34 | 55 | return RelativeBy({by: using})
|
35 | 56 |
|
36 | 57 |
|
37 | 58 | 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 | + """ |
40 | 83 | self.root = root
|
41 | 84 | self.filters = filters or []
|
42 | 85 |
|
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 | + """ |
44 | 92 | if not element_or_locator:
|
45 | 93 | raise WebDriverException("Element or locator must be given when calling above method")
|
46 | 94 |
|
47 | 95 | self.filters.append({"kind": "above", "args": [element_or_locator]})
|
48 | 96 | return self
|
49 | 97 |
|
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 | + """ |
51 | 104 | if not element_or_locator:
|
52 | 105 | raise WebDriverException("Element or locator must be given when calling above method")
|
53 | 106 |
|
54 | 107 | self.filters.append({"kind": "below", "args": [element_or_locator]})
|
55 | 108 | return self
|
56 | 109 |
|
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 | + """ |
58 | 116 | if not element_or_locator:
|
59 | 117 | raise WebDriverException("Element or locator must be given when calling above method")
|
60 | 118 |
|
61 | 119 | self.filters.append({"kind": "left", "args": [element_or_locator]})
|
62 | 120 | return self
|
63 | 121 |
|
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 | + """ |
65 | 128 | if not element_or_locator:
|
66 | 129 | raise WebDriverException("Element or locator must be given when calling above method")
|
67 | 130 |
|
68 | 131 | self.filters.append({"kind": "right", "args": [element_or_locator]})
|
69 | 132 | return self
|
70 | 133 |
|
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 | + """ |
72 | 140 | if not element_or_locator_distance:
|
73 | 141 | raise WebDriverException("Element or locator or distance must be given when calling above method")
|
74 | 142 |
|
75 | 143 | self.filters.append({"kind": "near", "args": [element_or_locator_distance]})
|
76 | 144 | return self
|
77 | 145 |
|
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 | + """ |
79 | 150 | return {
|
80 | 151 | 'relative': {
|
81 | 152 | 'root': self.root,
|
|
0 commit comments