0

I have a <section> tag in which there is <tfoot> tags and many <td> tags.

I want to store the 1st & 4th td elements of a in a Map, but i get index out of bounds exception.

Now, how can we use FindElement or FindElement method on a WebElement itself. Does this find all the child elements? Apologies as i have messed up all my code.

There are 14 tags on the page and i want to retrieve the text of td elements that are inside tfoot tag from each and every section, Product name and price, then store it in a String, String Map. Also, there is lot of whitespace in td element values.

<div>
                <table class="right">
                    <tfoot>
                        <tr class="product">
                            <td>Product 1
                            </td>
                            <td class="rt">323.00</td>
                            <td class="rt">
                                6
                            </td>
                            <td class="rt">
                                6
                            </td>
                            <td class="rt">&nbsp;
                            </td>
                        </tr>
                        </tfoot>
                </table>
            </div>

enter image description here List sectionsList = driver.findElements(By.tagName("section"));

Iterator<WebElement> it = sectionsList.iterator();
while(it.hasnext())
{
System.out.println(itr.next().findElements(By.tagName("tfoot")).get(1).findElements(By.tagName("td")).get(0)
                    .getText());

Now, i want to store the td values in a Hashmap.

Map<String, String> map1 = new HashMap<String, String>();


for(int i=1; i<sections.size()+1; i++){ map1.put(sectionList.get(i).findElements(By.tagName("tfoot")).get(1).findElements(By.tagName("td")).get(0).getText() , sectionList.get(0).findElements(By.tagName("tfoot")).get(1).findElements(By.tagName("td")).get(3).getText()); }
4
  • Post your HTML Dom code reference Jan 20, 2021 at 8:44
  • hi, done. thanks.
    – nick
    Jan 20, 2021 at 9:47
  • Please post it as code. Jan 20, 2021 at 10:19
  • done. it would be helpful if you could review the code.
    – nick
    Jan 20, 2021 at 10:45

1 Answer 1

1
for(int i=1; i<sections.size()+1; i++)

Why are you adding 1 to sections size, remove 1

for(int i=1; i<sections.size(); i++)

and also why starting from i=1 ? index starts from 0 to size-1 , so it should be i=0 ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.