Search code examples
selenium-webdriverxpathautomated-testsgoogle-chrome-devtools

Why I find two elements using xpath when I specify index?


I'm writing autotests. For finding elements I'm using XPATH. Now I need get list of element, as usual I write xpath which contains class name of table and childs elements. My page has 2 tables, and my XPATH can find both, but when I'm using index 1 I also find second table, when I'm using index 2 I find nothing.

How does it work? Why I can't choose second element

enter image description here


Solution

  • I'm pretty sure that the reason liesin the red part of your XPath.

    So in the red part of your XPath will have 2 elements with a table.

    To only select the first one you can put () around them like this:

    (//div[@class='with-some-classname']/following-sibling::table)[1]/tbody
    

    An other option would be to use the table as a predicate in your red part of the XPath like this i.e.:

    (//div[@class='with-some-classname'][following-sibling::table])[1]/following-sibling::table[1]/tbody