Is there an elegant way to get the By locator of a Selenium WebElement, that I already found/identified?
To be clear about the question: I want the "By locator" as used to find the element. I am in this case not interested in a specific attribute or a specific locator like the css-locator.
I know that I could parse the result of a WebElement's toString() method:
WebElement element = driver.findElement(By.id("myPreciousElement"));
System.out.println(element.toString());
Output would be for example:
[[FirefoxDriver: firefox on WINDOWS (....)] -> id: myPreciousElement]
if you found your element by xpath:
WebElement element = driver.findElement(By.xpath("//div[@someId = 'someValue']"));
System.out.println(element.toString());
Then your output will be:
[[FirefoxDriver: firefox on WINDOWS (....)] -> xpath: //div[@someId = 'someValue']]
So I currently wrote my own method that parses this output and gives me the "recreated" By locator.
BUT is there a more elegant way already implemented in Selenium to get the By locator used to find the element?
I couldn't find one so far.If you are sure, there is none out of the box, can you think of any reason why the API creators might not provide this functionality?
*Despite the fact that this has nothing to do with the question, if someone wonders why you would ever need this functionality, just 2 examples:
- if you use PageFactory you most likely will not have the locators as as member variables in your Page class, but you might need them later on when working with the page's elements.
- you're working with APIs of people who just use the Page Object Pattern without PageFactory and thus expect you to hand over locators instead of the element itself.*
Answer is No. You cannot extract a
By
from a previously found WebElement by default.That being said - it's possible to implement a custom solution, but Selenium does not offer this out-of-the-box.
Consider the following, on "why"..
you already have the
By
object, so you wouldn't need to call something likee.getBy()
I had written this utility function which returns a string combination of locator strategy + locator value.
Currently there is no specific method from selenium's end to do so. What you can do is write your custom method. You will get the clue of what selector type and path is used by just printing the webElement you have.
It looks something like this
Now, what you need to do is to extract the locator and its value. You can try something like this
`private By getByFromElement(WebElement element) {
`
For me worked with commons-lang3
For remote web element use method like:
No, there's not. I have implemented a possible solution as a proxy:
There is no elegant way provided by Selenium. And this is horrible
1)
PageObject
andPageFactory
implies that we haveWebElements
inPage
classes, but we don't have locators of those elements.2) If I find element as descendant of current element using
webElement.findElement(By)
, then I don't have the locator of this descendant even if I stored parent's locator in the variable.3) If I use
findElements
function that returnsList
of elemetns, then I don't have locator for each specific element.4) Having locator for element is useful at least because
ExpectedConditions
with locator as parameter are much better implemented thanExpectedConditions
withWebElement
as parameter.For me Selenium is ill-conceived and poorly implemented library