2016-05-11 39 views
5

Ho una lista di array da cui sto rilevando uno casuale. Posso stampare l'output casuale. Come passare l'output come valore xpath ??Come aggiungere l'output a xpath

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(); 
+0

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

risposta

2

provare questo.

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

grazie. Ma non ha funzionato. Mostra incapace di localizzare l'elemento. –

+0

Ha funzionato per dopo aver apportato le modifiche String xpath = "// * [@ name = '" + Category [index] + "']"; –

+0

@ArunKumar, ha aggiornato la risposta. Si prega di accettare/revocare se la risposta funziona per voi. – Gangaraju

0

Sarebbe più breve con un selettore CSS:

Random random = new Random(); 
int index = random.nextInt(Category.length); 
System.out.println(Category[index]); 
driver.findElement(By.cssSelector("[name='" + Category[index] + "']")).click();