可以从By获取定位字符串吗?

huangapple go评论83阅读模式
英文:

Can I get the locator string from a By?

问题

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']");

英文:

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!

答案1

得分: 2

以下是要翻译的内容:

我认为您可以按照以下方式执行,您可以使用toString()

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

selector的值将类似于这样 "By.cssSelector: a[id='add-article-btn']",然后您可以做的是,通过 ": " 将该值拆分。

String[] splittedValues = selector.split(": ");
String value = splittedValues1;

英文:

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];

huangapple
  • 本文由 发表于 2023年1月9日 17:31:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/75055318.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定