简体   繁体   中英

Selenium @FindBy find by one xpath or another

I'm trying to find an element on a page using

@FindBy(xpath = "somexpath")
WebElement someElement;

The problem is sometimes the element's xpath is slightly different (because of a failed login message that is displayed in the same table). How can I find an element by one xpath or another? Something like

xpath == "somexpath1" || xpath == "somepath2"

I've tried doing a repeating annotation, like this:

@FindBy(xpath = "somexpath1")
@FindBy(xpath = "somexpath2")
WebElement someElement

but that won't compile. And I've tried using @FindBys but that seems to work like && rather than ||.

Any help would be appreciated. Thanks!

Several paths can be combined with | separator, which effectively works as a logical OR.

@FindBy(xpath = "somexpath1 | somexpath2")

Try using like this :

@FindAll({ @FindBy(id = "id1"), @FindBy(id = "id2") })

If both id's are available you will get a list containing both elements else only one element either id1 or id2 .

It works fine for me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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