JSON creación de archivos utilizando para cada bucle y Mapa - Se repite el primer valor del elemento en todas las iteraciones sobre bucle for

día:

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
Fenio:

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 rowvariable 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 trelementos, en lugar de un índice específico. El selenio provoca a coincidir siempre con el primer elemento de la lista de todos los trs!

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");

Supongo que te gusta

Origin http://43.154.161.224:23101/article/api/json?id=227104&siteId=1
Recomendado
Clasificación