英文:
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];
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论