w3c / w3c/webdriver

Feature: Get element locator from WebElement

Open
#1,829 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement Find Element Find Element From Element Find Element From Shadow Root Find Elements From Element needs-discussion
Dominant language
HTML
Stars
718
Forks
228
PR merge metrics
No merged PRs in 30d

Description

Feature and motivation

Each WebElement must have a By locator that uniquely locates it.
As far as I can tell, the locator is protected and not available.
There are cases when pulling it out and re-using it would be helpful.
I propose exposing a getter for the By locator.

Usage example

You initialize a web element and it eventually becomes stale.
To refresh it, you either need to

  • remember the locator and re-load it by calling the locator. Not always possible if you pass the element around
  • use the @FindBy annotation so it auto refresh when stale. Can't use that if locators need to have variables.

If I need to check if an element exists

  • I used @FindBy, can't do that as it will patiently wait until it times out. Need to do a driver.findElementsBy(locator..).size()>0 , but can't as I can't reach the By to do this. So I copy paste my locator, once in annotation and once as a By in my fields, effectively duplicating code
  • if I do the same thing with a webelemennt I previous located, I can at least catch a stale exception and handle that, but would be nicer if I could get the by and fire up findelements to check if it still exists.

What if, instead, we could get the locator from each element. Would help with duplicating locators, as well as constructing new webelements or running the locators against the page again.

Opening after being told on the Selenium issues I should open one for the driver specification instead
https://github.com/SeleniumHQ/selenium/issues/13660

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the WebElement and By concepts in the WebDriver specification, then review the linked Selenium issue for the related implementation discussion. Determine whether the specification should expose a locator and define how it is retrieved and reused; the feature is done when that behavior is unambiguous in the specification.

Written by the indexing model from the issue text.

Assessment

Domain
api
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.