Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/py/selenium/webdriver/common/by.py
3985 views
1
# Licensed to the Software Freedom Conservancy (SFC) under one
2
# or more contributor license agreements. See the NOTICE file
3
# distributed with this work for additional information
4
# regarding copyright ownership. The SFC licenses this file
5
# to you under the Apache License, Version 2.0 (the
6
# "License"); you may not use this file except in compliance
7
# with the License. You may obtain a copy of the License at
8
#
9
# http://www.apache.org/licenses/LICENSE-2.0
10
#
11
# Unless required by applicable law or agreed to in writing,
12
# software distributed under the License is distributed on an
13
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
# KIND, either express or implied. See the License for the
15
# specific language governing permissions and limitations
16
# under the License.
17
"""The By implementation."""
18
19
from typing import Literal
20
21
ByType = Literal["id", "xpath", "link text", "partial link text", "name", "tag name", "class name", "css selector"]
22
23
24
class By:
25
"""Set of supported locator strategies.
26
27
ID:
28
--
29
Select the element by its ID.
30
31
>>> element = driver.find_element(By.ID, "myElement")
32
33
XPATH:
34
------
35
Select the element via XPATH.
36
- absolute path
37
- relative path
38
39
>>> element = driver.find_element(By.XPATH, "//html/body/div")
40
41
LINK_TEXT:
42
----------
43
Select the link element having the exact text.
44
45
>>> element = driver.find_element(By.LINK_TEXT, "myLink")
46
47
PARTIAL_LINK_TEXT:
48
------------------
49
Select the link element having the partial text.
50
51
>>> element = driver.find_element(By.PARTIAL_LINK_TEXT, "my")
52
53
NAME:
54
----
55
Select the element by its name attribute.
56
57
>>> element = driver.find_element(By.NAME, "myElement")
58
59
TAG_NAME:
60
--------
61
Select the element by its tag name.
62
63
>>> element = driver.find_element(By.TAG_NAME, "div")
64
65
CLASS_NAME:
66
-----------
67
Select the element by its class name.
68
69
>>> element = driver.find_element(By.CLASS_NAME, "myElement")
70
71
CSS_SELECTOR:
72
-------------
73
Select the element by its CSS selector.
74
75
>>> element = driver.find_element(By.CSS_SELECTOR, "div.myElement")
76
"""
77
78
ID: ByType = "id"
79
XPATH: ByType = "xpath"
80
LINK_TEXT: ByType = "link text"
81
PARTIAL_LINK_TEXT: ByType = "partial link text"
82
NAME: ByType = "name"
83
TAG_NAME: ByType = "tag name"
84
CLASS_NAME: ByType = "class name"
85
CSS_SELECTOR: ByType = "css selector"
86
87
_custom_finders: dict[str, str] = {}
88
89
@classmethod
90
def register_custom_finder(cls, name: str, strategy: str) -> None:
91
cls._custom_finders[name] = strategy
92
93
@classmethod
94
def get_finder(cls, name: str) -> str | None:
95
return cls._custom_finders.get(name) or getattr(cls, name.upper(), None)
96
97
@classmethod
98
def clear_custom_finders(cls) -> None:
99
cls._custom_finders.clear()
100
101