Path: blob/trunk/py/selenium/webdriver/common/by.py
3985 views
# Licensed to the Software Freedom Conservancy (SFC) under one1# or more contributor license agreements. See the NOTICE file2# distributed with this work for additional information3# regarding copyright ownership. The SFC licenses this file4# to you under the Apache License, Version 2.0 (the5# "License"); you may not use this file except in compliance6# with the License. You may obtain a copy of the License at7#8# http://www.apache.org/licenses/LICENSE-2.09#10# Unless required by applicable law or agreed to in writing,11# software distributed under the License is distributed on an12# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY13# KIND, either express or implied. See the License for the14# specific language governing permissions and limitations15# under the License.16"""The By implementation."""1718from typing import Literal1920ByType = Literal["id", "xpath", "link text", "partial link text", "name", "tag name", "class name", "css selector"]212223class By:24"""Set of supported locator strategies.2526ID:27--28Select the element by its ID.2930>>> element = driver.find_element(By.ID, "myElement")3132XPATH:33------34Select the element via XPATH.35- absolute path36- relative path3738>>> element = driver.find_element(By.XPATH, "//html/body/div")3940LINK_TEXT:41----------42Select the link element having the exact text.4344>>> element = driver.find_element(By.LINK_TEXT, "myLink")4546PARTIAL_LINK_TEXT:47------------------48Select the link element having the partial text.4950>>> element = driver.find_element(By.PARTIAL_LINK_TEXT, "my")5152NAME:53----54Select the element by its name attribute.5556>>> element = driver.find_element(By.NAME, "myElement")5758TAG_NAME:59--------60Select the element by its tag name.6162>>> element = driver.find_element(By.TAG_NAME, "div")6364CLASS_NAME:65-----------66Select the element by its class name.6768>>> element = driver.find_element(By.CLASS_NAME, "myElement")6970CSS_SELECTOR:71-------------72Select the element by its CSS selector.7374>>> element = driver.find_element(By.CSS_SELECTOR, "div.myElement")75"""7677ID: ByType = "id"78XPATH: ByType = "xpath"79LINK_TEXT: ByType = "link text"80PARTIAL_LINK_TEXT: ByType = "partial link text"81NAME: ByType = "name"82TAG_NAME: ByType = "tag name"83CLASS_NAME: ByType = "class name"84CSS_SELECTOR: ByType = "css selector"8586_custom_finders: dict[str, str] = {}8788@classmethod89def register_custom_finder(cls, name: str, strategy: str) -> None:90cls._custom_finders[name] = strategy9192@classmethod93def get_finder(cls, name: str) -> str | None:94return cls._custom_finders.get(name) or getattr(cls, name.upper(), None)9596@classmethod97def clear_custom_finders(cls) -> None:98cls._custom_finders.clear()99100101