Feature Proposal: Locators
🚀 Feature Proposal
Hi, I've never proposed a feature before so hopefully I can convey my idea clearly enough for people to think it useful to include in Selenium.
The proposal I have will help ease the burdensome and verbose calls to all the various different find_element_* and find_elements_* methods:
find_element_by_class_name
find_element_by_css_selector
find_element_by_id
find_element_by_link_text
find_element_by_name
find_element_by_partial_link_text
find_element_by_tag_name
find_element_by_xpath
# and again for `find_elements_*
I see the term "Locators" used throughout the Selenium documentation, but it never manifests itself as a real thing that users can use.
I've decided to make it real and I simply call this feature Locators.
This is something I've implemented in my own company's Python codebase. Here's how I did it. It's very simple.
"""locator.py"""
from collections import namedtuple
from selenium.webdriver.common.by import By
class Locator:
_ = namedtuple('Locator', ['by', 'query'])
@staticmethod
def css(query):
"""Locator by CSS selector."""
return Locator._(By.CSS_SELECTOR, query)
@staticmethod
def name(query):
"""Locator by name."""
return Locator._(By.NAME, query)
# and more for the other `By` types
Motivation
Too many find_* methods. So annoying!
Example
Here is Python code directly from https://selenium.dev/documentation/en/ (minus all the import lines):
Original code
with webdriver.Firefox() as driver:
wait = WebDriverWait(driver, 10)
driver.get("https://google.com/ncr")
driver.find_element_by_name("q").send_keys("cheese" + Keys.RETURN)
first_result = wait.until(presence_of_element_located((By.CSS_SELECTOR, "h3>div")))
print(first_result.get_attribute("textContent"))
With Locators
with webdriver.Firefox() as driver:
wait = WebDriverWait(driver, 10)
query_field = Locator.name("q")
div_element = Locator.css("h3>div")
driver.get("https://google.com/ncr")
driver.find_element(query_field).send_keys("cheese" + Keys.RETURN)
first_result = wait.until(presence_of_element_located(div_element))
print(first_result.get_attribute("textContent"))
You can see from this example that you no longer have to tell find what type of search strategy to use anymore. It's given when you define the locator. If changes to your website causes a change in your search strategy, you don't have to do a search and replace of all the various find_* methods. All you have to do is update the locator.
The namedtuple gives you the extra benefit of easily accessing by and query.
# given the example above
> div_element.by
'css selector'
> div_element.query
'h3>div'
I hope you can find the usefulness in this feature proposal. I'm sure it's not original and many people out there already do something like this, but it would be nice to see it built into Selenium.
I'll leave decision making to python binding maintainers, just want to note that the current implementation allows to use unnamed tuples:
with webdriver.Chrome() as driver:
wait = WebDriverWait(driver, 10)
query_field = (By.NAME, "q")
div_element = (By.CSS_SELECTOR, "h3>div")
driver.get("https://google.com/ncr")
driver.find_element(*query_field).send_keys("cheese" + Keys.RETURN)
first_result = wait.until(presence_of_element_located(div_element))
print(first_result.get_attribute("textContent"))
So the only benefit from the proposed feature is an ability to use named tuples instead of unnamed ones.
@barancev I realize unnamed tuples work, but it just doesn't look as elegant. The locator feature I'm proposing also tightly binds the search strategy and search query together into a single object. I think this is a much better practice than having them loosely coupled together (unnamed tuples) or not having them coupled at all (as seen in the Selenium "Getting Started" docs).
I am not totally against this idea, I think it's useful for locators where you need to update a part of it like row = (By.XPATH, '//div/tr[{}]') and then in your code you write find_element(row.locator.format(index)). But if you want something a bit more dynamic, you would have to write a lot more code to deal with the tuple-ness. Or maybe just get rid of the tuple altogether and make a custom class that can "just work".
But it's a bit too late to change the API to make this native.
If you really want this pattern, I agree with @barancev, tuples do the trick. It's more readable too, but that's just my opinion. This seems like something more suited to a Page Object library than Selenium itself.
As part of Selenium 4 we are going to be deprecating the other locator strategies and documentation will be updated. As @barancev pointed out you can already use tuples and a lot of users do that already.
deprecation in 264ed97
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.