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
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 tr
s!
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");
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.