JSON File Creation using for each loop And Map - It repeats the first element value on all iterations over for loop

Roja :

For each loop below not adding all 6 values from the list. Instead It just repeats the first value 6times. Also Json file created with the same inputs.

Note: I also referred to this link but not getting the expected output. Javascript each loop over JSON only getting first element?

Steps followed are, 1. Find the tr elements and read data one by one 2. While reading store these values also using Map 3. Add the map values into Jason Array.

Below is my code,

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();
}

Expected Output should be:

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

Actual Output of the above code is:

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 :

The solution is simple. You have incorrect XPATH

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

How you pass row variable is crucial!

You pass it as a String (in context of Xpath), instead of a digit.

Let's assume that row=1

Your Xpath looks like this:

String rowKeys=tablerow.findElement(By.xpath("//div[@class='content shadow']/div/child::table[1]/tbody/tr['1']/td[1]")).getText();

Do you see it now? You create tr['1'] instead of tr[1] and YES - it does matter. If you pass it as tr['1'] then you will match ALL of tr elements, instead of a specific index. It causes Selenium to match always the first element from the list of all trs!

Just change your Xpaths to:

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

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=122019&siteId=1