Skip to content

Commit 080a63e

Browse files
committed
[py] Support RelativeBy in type annotations
1 parent b3fc1b3 commit 080a63e

File tree

4 files changed

+36
-24
lines changed

4 files changed

+36
-24
lines changed

py/selenium/webdriver/remote/shadowroot.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,12 @@
1616
# under the License.
1717

1818
from hashlib import md5 as md5_hash
19+
from typing import List, Optional, Union
1920

20-
from ..common.by import By
21+
from ..common.by import By, ByType
22+
from ..support.relative_locator import RelativeBy
2123
from .command import Command
24+
from .webelement import WebElement
2225

2326

2427
class ShadowRoot:
@@ -43,7 +46,9 @@ def __repr__(self) -> str:
4346
def id(self) -> str:
4447
return self._id
4548

46-
def find_element(self, by: str = By.ID, value: str = None):
49+
def find_element(
50+
self, by: Union[ByType, RelativeBy] = By.ID, value: Optional[str] = None,
51+
) -> WebElement:
4752
"""Find an element inside a shadow root given a By strategy and
4853
locator.
4954
@@ -82,7 +87,9 @@ def find_element(self, by: str = By.ID, value: str = None):
8287

8388
return self._execute(Command.FIND_ELEMENT_FROM_SHADOW_ROOT, {"using": by, "value": value})["value"]
8489

85-
def find_elements(self, by: str = By.ID, value: str = None):
90+
def find_elements(
91+
self, by: Union[ByType, RelativeBy] = By.ID, value: Optional[str] = None,
92+
) -> List[WebElement]:
8693
"""Find elements inside a shadow root given a By strategy and locator.
8794
8895
Parameters:

py/selenium/webdriver/remote/webdriver.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
from selenium.webdriver.common.bidi.script import Script
4646
from selenium.webdriver.common.bidi.session import Session
4747
from selenium.webdriver.common.bidi.storage import Storage
48-
from selenium.webdriver.common.by import By
48+
from selenium.webdriver.common.by import By, ByType
4949
from selenium.webdriver.common.options import ArgOptions, BaseOptions
5050
from selenium.webdriver.common.print_page_options import PrintOptions
5151
from selenium.webdriver.common.timeouts import Timeouts
@@ -875,7 +875,7 @@ def timeouts(self, timeouts) -> None:
875875
"""
876876
_ = self.execute(Command.SET_TIMEOUTS, timeouts._to_json())["value"]
877877

878-
def find_element(self, by=By.ID, value: Optional[str] = None) -> WebElement:
878+
def find_element(self, by: Union[ByType, RelativeBy] = By.ID, value: Optional[str] = None) -> WebElement:
879879
"""Find an element given a By strategy and locator.
880880
881881
Parameters:
@@ -911,7 +911,7 @@ def find_element(self, by=By.ID, value: Optional[str] = None) -> WebElement:
911911

912912
return self.execute(Command.FIND_ELEMENT, {"using": by, "value": value})["value"]
913913

914-
def find_elements(self, by=By.ID, value: Optional[str] = None) -> List[WebElement]:
914+
def find_elements(self, by: Union[ByType, RelativeBy] = By.ID, value: Optional[str] = None) -> List[WebElement]:
915915
"""Find elements given a By strategy and locator.
916916
917917
Parameters:

py/selenium/webdriver/support/event_firing_webdriver.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,16 @@
1515
# specific language governing permissions and limitations
1616
# under the License.
1717

18-
from typing import Any, List, Tuple
18+
from typing import Any, List, Optional, Tuple, Union
1919

2020
from selenium.common.exceptions import WebDriverException
2121
from selenium.webdriver.common.by import By
22+
from selenium.webdriver.common.by import ByType
2223
from selenium.webdriver.remote.webdriver import WebDriver
2324
from selenium.webdriver.remote.webelement import WebElement
2425

2526
from .abstract_event_listener import AbstractEventListener
27+
from .relative_locator import RelativeBy
2628

2729

2830
def _wrap_elements(result, ef_driver):
@@ -187,10 +189,10 @@ def clear(self) -> None:
187189
def send_keys(self, *value) -> None:
188190
self._dispatch("change_value_of", (self._webelement, self._driver), "send_keys", value)
189191

190-
def find_element(self, by=By.ID, value=None) -> WebElement:
192+
def find_element(self, by: Union[ByType, RelativeBy] = By.ID, value: Optional[str] = None) -> WebElement:
191193
return self._dispatch("find", (by, value, self._driver), "find_element", (by, value))
192194

193-
def find_elements(self, by=By.ID, value=None) -> List[WebElement]:
195+
def find_elements(self, by: Union[ByType, RelativeBy] = By.ID, value: Optional[str] = None) -> List[WebElement]:
194196
return self._dispatch("find", (by, value, self._driver), "find_elements", (by, value))
195197

196198
def _dispatch(self, l_call, l_args, d_call, d_args):

py/selenium/webdriver/support/expected_conditions.py

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@
2727
WebDriverException,
2828
)
2929
from selenium.webdriver.common.alert import Alert
30+
from selenium.webdriver.common.by import ByType
3031
from selenium.webdriver.remote.webdriver import WebDriver, WebElement
32+
from selenium.webdriver.support.relative_locator import RelativeBy
3133

3234
"""
3335
* Canned "Expected Conditions" which are generally useful within webdriver
@@ -38,6 +40,7 @@
3840
T = TypeVar("T")
3941

4042
WebDriverOrWebElement = Union[WebDriver, WebElement]
43+
LocatorType = Union[Tuple[ByType, str], Tuple[RelativeBy, None]]
4144

4245

4346
def title_is(title: str) -> Callable[[WebDriver], bool]:
@@ -79,7 +82,7 @@ def _predicate(driver: WebDriver):
7982
return _predicate
8083

8184

82-
def presence_of_element_located(locator: Tuple[str, str]) -> Callable[[WebDriverOrWebElement], WebElement]:
85+
def presence_of_element_located(locator: LocatorType) -> Callable[[WebDriverOrWebElement], WebElement]:
8386
"""An expectation for checking that an element is present on the DOM of a
8487
page. This does not necessarily mean that the element is visible.
8588
@@ -189,7 +192,7 @@ def _predicate(driver: WebDriver):
189192

190193

191194
def visibility_of_element_located(
192-
locator: Tuple[str, str],
195+
locator: LocatorType,
193196
) -> Callable[[WebDriverOrWebElement], Union[Literal[False], WebElement]]:
194197
"""An expectation for checking that an element is present on the DOM of a
195198
page and visible. Visibility means that the element is not only displayed
@@ -272,7 +275,7 @@ def _element_if_visible(element: WebElement, visibility: bool = True) -> Union[L
272275
return element if element.is_displayed() == visibility else False
273276

274277

275-
def presence_of_all_elements_located(locator: Tuple[str, str]) -> Callable[[WebDriverOrWebElement], List[WebElement]]:
278+
def presence_of_all_elements_located(locator: LocatorType) -> Callable[[WebDriverOrWebElement], List[WebElement]]:
276279
"""An expectation for checking that there is at least one element present
277280
on a web page.
278281
@@ -299,7 +302,7 @@ def _predicate(driver: WebDriverOrWebElement):
299302
return _predicate
300303

301304

302-
def visibility_of_any_elements_located(locator: Tuple[str, str]) -> Callable[[WebDriverOrWebElement], List[WebElement]]:
305+
def visibility_of_any_elements_located(locator: LocatorType) -> Callable[[WebDriverOrWebElement], List[WebElement]]:
303306
"""An expectation for checking that there is at least one element visible
304307
on a web page.
305308
@@ -327,7 +330,7 @@ def _predicate(driver: WebDriverOrWebElement):
327330

328331

329332
def visibility_of_all_elements_located(
330-
locator: Tuple[str, str],
333+
locator: LocatorType,
331334
) -> Callable[[WebDriverOrWebElement], Union[List[WebElement], Literal[False]]]:
332335
"""An expectation for checking that all elements are present on the DOM of
333336
a page and visible. Visibility means that the elements are not only
@@ -363,7 +366,7 @@ def _predicate(driver: WebDriverOrWebElement):
363366
return _predicate
364367

365368

366-
def text_to_be_present_in_element(locator: Tuple[str, str], text_: str) -> Callable[[WebDriverOrWebElement], bool]:
369+
def text_to_be_present_in_element(locator: LocatorType, text_: str) -> Callable[[WebDriverOrWebElement], bool]:
367370
"""An expectation for checking if the given text is present in the
368371
specified element.
369372
@@ -399,7 +402,7 @@ def _predicate(driver: WebDriverOrWebElement):
399402

400403

401404
def text_to_be_present_in_element_value(
402-
locator: Tuple[str, str], text_: str
405+
locator: LocatorType, text_: str
403406
) -> Callable[[WebDriverOrWebElement], bool]:
404407
"""An expectation for checking if the given text is present in the
405408
element's value.
@@ -436,7 +439,7 @@ def _predicate(driver: WebDriverOrWebElement):
436439

437440

438441
def text_to_be_present_in_element_attribute(
439-
locator: Tuple[str, str], attribute_: str, text_: str
442+
locator: LocatorType, attribute_: str, text_: str
440443
) -> Callable[[WebDriverOrWebElement], bool]:
441444
"""An expectation for checking if the given text is present in the
442445
element's attribute.
@@ -477,7 +480,7 @@ def _predicate(driver: WebDriverOrWebElement):
477480

478481

479482
def frame_to_be_available_and_switch_to_it(
480-
locator: Union[Tuple[str, str], str, WebElement],
483+
locator: Union[LocatorType, str, WebElement],
481484
) -> Callable[[WebDriver], bool]:
482485
"""An expectation for checking whether the given frame is available to
483486
switch to.
@@ -517,7 +520,7 @@ def _predicate(driver: WebDriver):
517520

518521

519522
def invisibility_of_element_located(
520-
locator: Union[WebElement, Tuple[str, str]],
523+
locator: Union[WebElement, LocatorType],
521524
) -> Callable[[WebDriverOrWebElement], Union[WebElement, bool]]:
522525
"""An Expectation for checking that an element is either invisible or not
523526
present on the DOM.
@@ -565,7 +568,7 @@ def _predicate(driver: WebDriverOrWebElement):
565568

566569

567570
def invisibility_of_element(
568-
element: Union[WebElement, Tuple[str, str]],
571+
element: Union[WebElement, LocatorType],
569572
) -> Callable[[WebDriverOrWebElement], Union[WebElement, bool]]:
570573
"""An Expectation for checking that an element is either invisible or not
571574
present on the DOM.
@@ -592,7 +595,7 @@ def invisibility_of_element(
592595

593596

594597
def element_to_be_clickable(
595-
mark: Union[WebElement, Tuple[str, str]],
598+
mark: Union[WebElement, LocatorType],
596599
) -> Callable[[WebDriverOrWebElement], Union[Literal[False], WebElement]]:
597600
"""An Expectation for checking an element is visible and enabled such that
598601
you can click it.
@@ -687,7 +690,7 @@ def _predicate(_):
687690
return _predicate
688691

689692

690-
def element_located_to_be_selected(locator: Tuple[str, str]) -> Callable[[WebDriverOrWebElement], bool]:
693+
def element_located_to_be_selected(locator: LocatorType) -> Callable[[WebDriverOrWebElement], bool]:
691694
"""An expectation for the element to be located is selected.
692695
693696
Parameters:
@@ -743,7 +746,7 @@ def _predicate(_):
743746

744747

745748
def element_located_selection_state_to_be(
746-
locator: Tuple[str, str], is_selected: bool
749+
locator: LocatorType, is_selected: bool
747750
) -> Callable[[WebDriverOrWebElement], bool]:
748751
"""An expectation to locate an element and check if the selection state
749752
specified is in that state.
@@ -858,7 +861,7 @@ def _predicate(driver: WebDriver):
858861
return _predicate
859862

860863

861-
def element_attribute_to_include(locator: Tuple[str, str], attribute_: str) -> Callable[[WebDriverOrWebElement], bool]:
864+
def element_attribute_to_include(locator: LocatorType, attribute_: str) -> Callable[[WebDriverOrWebElement], bool]:
862865
"""An expectation for checking if the given attribute is included in the
863866
specified element.
864867

0 commit comments

Comments
 (0)