Path: blob/trunk/py/selenium/webdriver/common/by.py
1864 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 Literal, Optional192021class By:22"""Set of supported locator strategies.2324ID:25--26Select the element by its ID.2728>>> element = driver.find_element(By.ID, "myElement")2930XPATH:31------32Select the element via XPATH.33- absolute path34- relative path3536>>> element = driver.find_element(By.XPATH, "//html/body/div")3738LINK_TEXT:39----------40Select the link element having the exact text.4142>>> element = driver.find_element(By.LINK_TEXT, "myLink")4344PARTIAL_LINK_TEXT:45------------------46Select the link element having the partial text.4748>>> element = driver.find_element(By.PARTIAL_LINK_TEXT, "my")4950NAME:51----52Select the element by its name attribute.5354>>> element = driver.find_element(By.NAME, "myElement")5556TAG_NAME:57--------58Select the element by its tag name.5960>>> element = driver.find_element(By.TAG_NAME, "div")6162CLASS_NAME:63-----------64Select the element by its class name.6566>>> element = driver.find_element(By.CLASS_NAME, "myElement")6768CSS_SELECTOR:69-------------70Select the element by its CSS selector.7172>>> element = driver.find_element(By.CSS_SELECTOR, "div.myElement")73"""7475ID = "id"76XPATH = "xpath"77LINK_TEXT = "link text"78PARTIAL_LINK_TEXT = "partial link text"79NAME = "name"80TAG_NAME = "tag name"81CLASS_NAME = "class name"82CSS_SELECTOR = "css selector"8384_custom_finders: dict[str, str] = {}8586@classmethod87def register_custom_finder(cls, name: str, strategy: str) -> None:88cls._custom_finders[name] = strategy8990@classmethod91def get_finder(cls, name: str) -> Optional[str]:92return cls._custom_finders.get(name) or getattr(cls, name.upper(), None)9394@classmethod95def clear_custom_finders(cls) -> None:96cls._custom_finders.clear()979899ByType = Literal["id", "xpath", "link text", "partial link text", "name", "tag name", "class name", "css selector"]100101102