Arun Kumar
Arun Kumar

Reputation: 139

How to add the output to xpath

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

Answers (2)

Gangaraju
Gangaraju

Reputation: 4562

Try this one.

String xpath= "//*[@name='" + Category[index] + "']";   
driver.findElement(By.xpath(xpath)).click();

Upvotes: 2

Florent B.
Florent B.

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

Related Questions