简体   繁体   English

Selenium Java和PageObjectModel用于通过包含字符串的id查找元素

[英]Selenium Java and PageObjectModel for finding element by id that contains string

I have a Java project using Selenium and Page Object Model and need to find buttons with IDs that end with the String "Cancel". 我有一个使用Selenium和Page Object Model的Java项目,需要查找ID以字符串“ Cancel”结尾的按钮。 I tried using regular expressions also, I found a few solutions on stackoverflow that included XPath, but taking into consideration that the website's design is changing often I do not use XPath. 我也尝试使用正则表达式,但在stackoverflow上找到了一些包含XPath的解决方案,但考虑到网站的设计经常发生变化,因此我不使用XPath。 I also found as a solution that you can use an ends-with CSS selector: 我还找到了一种可以使用end-with CSS选择器的解决方案:

By.cssSelector("[id$=default-create-firstname]") but I would like to take advantage of the Page Object Model and use the annotation @FindBy , therefore omitting the By selectors. By.cssSelector("[id$=default-create-firstname]")但我想利用页面对象模型并使用注释@FindBy ,因此省略了By选择器。

@FindBy(id = "ButtonToCancel")
private WebElement buttonToCancel;

How can I select all the IDs in the page that end in *Cancel, without hardcoding each id find @FindBy? 如何在* Cancel结尾的页面中选择所有ID,而无需对每个ID进行硬编码找到@FindBy? From what I know, Regex patterns do not work as such: @FindBy(id="*Cancel") 据我所知,正则表达式模式不能这样工作:@FindBy(id =“ * Cancel”)

Try this: 尝试这个:

@FindBy(css = "*[id$='cancel']"
private List<WebElement> cancelButtons;

It will return list of WebElements with id ended with " cancel " in id field. 它将返回ID字段中ID以“ cancel ”结尾的WebElement列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM