Para cada bucle a continuación no adición de los 6 valores de la lista. En su lugar, simplemente repite las primeras 6times valor. Además archivo JSON creado con las mismas entradas.
Nota: También me he referido a este vínculo, pero no obtener el resultado esperado. Javascript cada bucle sobre JSON sólo para conseguir primer elemento?
Pasos a seguir son: 1. Buscar los elementos TR y leer datos uno por uno 2. Mientras tienda de leer estos valores también usando Mapa 3. Añadir los valores de mapa en Jason matriz.
A continuación se muestra el código,
public static void GetPercentageValue(String locatorType, String locatorValue) throws FileNotFoundException
{
locatorType="xpath";
locatorValue="//div[@class='content shadow']/div/child::table[1]/tbody/tr";
List<WebElement> tableRows=findTheElements(locatorType,locatorValue);
for(WebElement tablerow : tableRows)
{
Map map = new LinkedHashMap(tableRows.size());
row=row+1;
String rowKeys=tablerow.findElement(By.xpath("//div[@class='content shadow']/div/child::table[1]/tbody/tr['"+row+"']/td[1]")).getText();
String price=tablerow.findElement(By.xpath("//div[@class='content shadow']/div/child::table[1]/tbody/tr['"+row+"']//input")).getAttribute("value");
System.out.println("RowDesc & Price: "+rowKeys+" "+price);
map.put(rowKeys, price);
jArr.add(map);
}
jaObj.put("Values", jArr);
PrintWriter pWriter = new PrintWriter(mbsDataPath1);
pWriter.write(jaObj.toJSONString());
pWriter.flush();
pWriter.close();
}
Resultados esperados debe ser:
RowDesc & Price: <80.000 0
RowDesc & Price: 80.000-150.000 2
RowDesc & Price: 150.000-300.000 10
RowDesc & Price: 300.000-500.000 15
RowDesc & Price: >500.000 18
La salida real del código anterior es:
RowDesc & Price: <80.000 0
RowDesc & Price: <80.000 0
RowDesc & Price: <80.000 0
RowDesc & Price: <80.000 0
RowDesc & Price: <80.000 0
La solución es simple. Tiene XPATH incorrectos
String rowKeys=tablerow.findElement(By.xpath("//div[@class='content shadow']/div/child::table[1]/tbody/tr['"+row+"']/td[1]")).getText();
String price=tablerow.findElement(By.xpath("//div[@class='content shadow']/div/child::table[1]/tbody/tr['"+row+"']//input")).getAttribute("value");
¿Cómo se pasa row
variable es crucial!
Se le pasa como una cadena (en el contexto de XPath), en lugar de un dígito.
Vamos a suponer que row=1
Su Xpath se ve así:
String rowKeys=tablerow.findElement(By.xpath("//div[@class='content shadow']/div/child::table[1]/tbody/tr['1']/td[1]")).getText();
lo ves ahora? Se crea tr['1']
en lugar de tr[1]
y SI - Sí importa. Si se le pasa como tr['1']
a continuación, va a coincidir con TODOS los tr
elementos, en lugar de un índice específico. El selenio provoca a coincidir siempre con el primer elemento de la lista de todos los tr
s!
Sólo cambia sus XPaths a:
String rowKeys=tablerow.findElement(By.xpath("//div[@class='content shadow']/div/child::table[1]/tbody/tr["+row+"]/td[1]")).getText();
String price=tablerow.findElement(By.xpath("//div[@class='content shadow']/div/child::table[1]/tbody/tr["+row+"]//input")).getAttribute("value");