-1

I am a newbie in Selenium and Java.

  • Now, is this code correct. How can I call findElement() or findElements() method on a single WebElement? I want to store text of td[1] node.
  • Why are we doing Method chaining here?
    List<WebElement> sections = driver.findElements(By.tagname("section"));
    String td1 = sections.get(0).findElements(By.tagName("tfoot")).get(1).findElements(By.tagName("td")).get(0).getText());

Html DOM code:

    <section>
        <h2>product table</h2>
        <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>
        </section>
5
  • code looks correct what is the issue you facing – PDHide Jan 21 at 8:54
  • I want to understand the logic behind calling findElement method on a WebElement, instead of driver reference. – Karl67 Jan 21 at 10:38
  • Did the answer help ? – PDHide Jan 21 at 13:47
  • Yes, thanks for the answer. – Karl67 Jan 21 at 19:02
  • Please accept it by pressing the tick sign near to it – PDHide Jan 21 at 19:04
0

you can find a element as

elem = driver.findElement(somelocator) or
elem.findElement(someLocator)

Now consider a use case where you have to find all options inside a header

enter image description here

here the header locator is:

//ol[@role="presentation"]

Now as the icon are children of the header element and header element is unique , we can use this as reference to find all the child elements

eg:

   //ol[@role="presentation"]/li[3]
   //ol[@role="presentation"]/li[4] ... and so on 

enter image description here

now you can use locator as :

 elem = driver.findElements(By.xpath("//ol[@role=\"presentation\"]/li"))[3] or
 elem = driver.findElement(By.xpath("//ol[@role=\"presentation\"]/li[3]"))

Another good option is to find the header element and call child from it

 header = driver.findElement(By.xpath("//ol[@role=\"presentation\"]"))
 elem   = header.findElements(By.tagname("li"))[3]

here it does the same thing calls all child elements of header tag , this look more cleaner

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