Home > Enterprise >  Can I get the locator string from a By?
Can I get the locator string from a By?

Time:01-11

At the moment, I'm storing references to page elements as Bys:

public By showAllCheckbox = By.Id("show-all");
public By addArticleButton = By.CssSelector("a[id='add-article-btn']");
public By exampleEventCategoryEntityPickerPlusButton = By.XPath("//*[@id='j1_1']/i");
public By exampleEventReasonEntityPickerEntity = By.CssSelector("a[id='7603_anchor']");

Using selenium (or some other witchcraft) is it possible to get that selector string back out of the By? For example, for the add article button above I would want to get "a[id='add-article-btn']".

Thanks!

CodePudding user response:

I think you can do it as follows, You can use toString()

By addArtclBtn = By.CssSelector("a[id='add-article-btn']");
String selector = addArtclBtn.toString();

selector value will be something like this "By.cssSelector: a[id='add-article-btn']", Then what you can do is, just spliit the value by ": ".

String[] splittedValues = selector.split(": ");
String value = splittedValues[1];
  • Related