selenium icon indicating copy to clipboard operation
selenium copied to clipboard

[🚀 Feature]: Get element locator from WebElement

Open skyhirider opened this issue 1 year ago • 1 comments

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.

skyhirider avatar Mar 06 '24 17:03 skyhirider

@skyhirider, thank you for creating this issue. We will troubleshoot it as soon as we can.


Info for maintainers

Triage this issue by using labels.

If information is missing, add a helpful comment and then I-issue-template label.

If the issue is a question, add the I-question label.

If the issue is valid but there is no time to troubleshoot it, consider adding the help wanted label.

If the issue requires changes or fixes from an external project (e.g., ChromeDriver, GeckoDriver, MSEdgeDriver, W3C), add the applicable G-* label, and it will provide the correct link and auto-close the issue.

After troubleshooting the issue, please add the R-awaiting answer label.

Thank you!

github-actions[bot] avatar Mar 06 '24 17:03 github-actions[bot]

Hi, @skyhirider. This issue has been determined to require a change to the WebDriver W3C Specification for Selenium to be able to support it.

Please create an issue with the WebDriver project.

Feel free to comment the issues that you raise back in this issue. Thank you.

github-actions[bot] avatar Jul 12 '24 08:07 github-actions[bot]

This issue has been automatically locked since there has not been any recent activity since it was closed. Please open a new issue for related bugs.

github-actions[bot] avatar Aug 11 '24 22:08 github-actions[bot]