Reputation: 139
I have list of arrays from which I am picking up a random one. I can print the random output. How to pass the output as xpath value??
String[] Category = {"abc", "abc", "abc", "abc", "abc", "abc", "abc"};
Random random = new Random();
int index = random.nextInt(Category.length);
System.out.println(Category[index]);
driver.findElement(By.xpath("//*[@name='\"${Category[index]}\"']")).click();
Upvotes: 5
Views: 100
Reputation: 4562
Try this one.
String xpath= "//*[@name='" + Category[index] + "']";
driver.findElement(By.xpath(xpath)).click();
Upvotes: 2
Reputation: 42528
It would be shorter with a CSS selector:
Random random = new Random();
int index = random.nextInt(Category.length);
System.out.println(Category[index]);
driver.findElement(By.cssSelector("[name='" + Category[index] + "']")).click();
Upvotes: 0