-1

I have multiple products on the Screen, each of them differs from the string "ProductName". Instead of collecting all the products onto the page. I want to make any dynamic or reusable function so that I can pass the product name on the function and it should recognize the element

public findElement(String ProductName)
{
     element= By.xpath("//a[contains(text(),'ProductName')]");
}

If I pass the product name to Apple, then it should give me the locator as

element= By.xpath("//a[contains(text(),'apple')]");
Navjot
  • 1,202
  • 1
  • 11
  • 24

1 Answers1

0

You can do something like following:

public findElement(List<String> products)
{
   for(String product : products){
       String xpath = String.format("//a[contains(text(),'%s')]",product);
       element= By.xpath(xpath);
   }

}
Prophet
  • 32,350
  • 22
  • 54
  • 79