5

I'm using WebdriverIO to drive my Selenium server, but I was curious if there is a guarantee in the Selenium spec, or in the CSS spec that guarantees elements are returned in the order they are found appearing on the page.

For instance:

<ul>
 <li>One</li>
 <li>Two</li>
 <li>Three</li>
</ul>

If I were to do driver.getText('li');, am I guaranteed that it will return:

['One', 'Two', 'Three']

Or are there cases where the behavior will be undefined? Maybe also, what sort of behavior should I expect? I suppose it's probably a depth first search?

alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
Breedly
  • 12,838
  • 13
  • 59
  • 83

1 Answers1

5

Yes, the order is guaranteed by the WebDriver specification:

All element location strategies must return elements in the order in which they appear in the current document.

alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • Tremendous, I'll have to read this top to bottom. Interesting how that also answers whether it's a breadth or depth first traversal. – Breedly May 27 '15 at 18:05
  • Wow! that's one of the questions I had too. Thanks to both of you – Saifur May 27 '15 at 18:05
  • @Breedly FYI, I've fixed the link (now it points to a fresh spec). Thanks. – alecxe May 27 '15 at 18:10
  • Yeah, the specification helps a lot in understanding how webdriver works internally. For instance, I had a nice journey into how visibility of an element is determined: https://w3c.github.io/webdriver/webdriver-spec.html#element-displayedness. – alecxe May 27 '15 at 18:11
  • 1
    @alecxe I wonder if the pop up windows are also ordered accordingly – Saifur May 27 '15 at 18:15
  • @Saifur interesting, do you mean the order of the window handles in the list? – alecxe May 27 '15 at 18:16
  • Correct. I actually encountered an issue with window handles recently where I did not want to close the original window handle but the rest. I had to write some junky code just to make sure that I do not close the original. I hope the order is guaranteed – Saifur May 27 '15 at 18:19
  • 1
    @Saifur yeah, no order for window handles: it says `The Get Window Handle command returns a list of window handles for every open top-level browsing context. The order in which the window handles are returned is arbitary.` – alecxe May 27 '15 at 18:31