@Findby using xpath with index

Prakash P picture Prakash P · Jun 3, 2016 · Viewed 13.5k times · Source

Can I use @Findby and pass certain values as parameter?

@FindBy("//div[contains(@class,'gallery_grid_image_caption gallery_grid_image_caption_padding')]"[$INDEX])

I know I can do this while using findElement. Kindly let me know if there is a solution/work around.

What I want to do it is let's say there is a for loop and there is a list of elements in a page. Now let's say the only thing that is changing among these fields is the bit of the xpath. //div/1, //div/2 .... What I want to do is represent one element for all these elements and pass the ending values as parameter.

Answer

Naman picture Naman · Jun 3, 2016

I believe what you are trying to do is something like this :

@FindBy(xpath = "//div[contains(@class,'gallery_grid_image_caption gallery_grid_image_caption_padding')]")
public WebElement yourElement;

SO-9028757 should provide you more context.