Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/py/selenium/webdriver/common/by.py
1864 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, Optional
20
21
22
class By:
23
"""Set of supported locator strategies.
24
25
ID:
26
--
27
Select the element by its ID.
28
29
>>> element = driver.find_element(By.ID, "myElement")
30
31
XPATH:
32
------
33
Select the element via XPATH.
34
- absolute path
35
- relative path
36
37
>>> element = driver.find_element(By.XPATH, "//html/body/div")
38
39
LINK_TEXT:
40
----------
41
Select the link element having the exact text.
42
43
>>> element = driver.find_element(By.LINK_TEXT, "myLink")
44
45
PARTIAL_LINK_TEXT:
46
------------------
47
Select the link element having the partial text.
48
49
>>> element = driver.find_element(By.PARTIAL_LINK_TEXT, "my")
50
51
NAME:
52
----
53
Select the element by its name attribute.
54
55
>>> element = driver.find_element(By.NAME, "myElement")
56
57
TAG_NAME:
58
--------
59
Select the element by its tag name.
60
61
>>> element = driver.find_element(By.TAG_NAME, "div")
62
63
CLASS_NAME:
64
-----------
65
Select the element by its class name.
66
67
>>> element = driver.find_element(By.CLASS_NAME, "myElement")
68
69
CSS_SELECTOR:
70
-------------
71
Select the element by its CSS selector.
72
73
>>> element = driver.find_element(By.CSS_SELECTOR, "div.myElement")
74
"""
75
76
ID = "id"
77
XPATH = "xpath"
78
LINK_TEXT = "link text"
79
PARTIAL_LINK_TEXT = "partial link text"
80
NAME = "name"
81
TAG_NAME = "tag name"
82
CLASS_NAME = "class name"
83
CSS_SELECTOR = "css selector"
84
85
_custom_finders: dict[str, str] = {}
86
87
@classmethod
88
def register_custom_finder(cls, name: str, strategy: str) -> None:
89
cls._custom_finders[name] = strategy
90
91
@classmethod
92
def get_finder(cls, name: str) -> Optional[str]:
93
return cls._custom_finders.get(name) or getattr(cls, name.upper(), None)
94
95
@classmethod
96
def clear_custom_finders(cls) -> None:
97
cls._custom_finders.clear()
98
99
100
ByType = Literal["id", "xpath", "link text", "partial link text", "name", "tag name", "class name", "css selector"]
101
102