Search code examples
angularjsselenium-webdriverautomated-teststestcomplete

Selenium DOM Search Order


I know this question can be tricky, would take help/suggestions. if duplicate/bad , pls lemme know via comments I will delete.

I would like to understand how Selenium searches within the tree structure of a DOM.

(The reason why i asked is i switched from Testcomplete just now. Testcomplete searches from the bottom up. The bottom most elements gets searched faster. Also if there are 10 elements with same id, and we try findelement and not findallelements, it will give the bottom most element)

So Selenium - does it start from top or bottom of a tree branch?. Let's see the tree is as below

<div>  
    <section1>
        <h1>
        </h1>
        <h2>
        </h2>
   </section1>
    <section2>
        <i1>
        </i1>
   </section2>
   <section3>
        <j1>
            <k1 id='something'>
            </k1>
        </j1>
        <j2>
        </j2>
   </section3>
</div>

I am searching for Element id = something.

  1. Does it first look out for Section 1, h1 tag and then traverses down ? or it starts from Section 3 j2 tag and starts upwards?. The reason is, in a very lengthy AngularJs page, searching for a table at the top gives faster results and a table in the bottom takes around 15 seconds almost.
  2. Second - does it search like serially ? or Vertically?.

    Serially - Search Section1, h1, h2, section2, i1 etc.

    Vertically - Search Section1, section2, section3 and then h1,h2,i1 etc.

Understanding this would go a long way in establishing a good element locator strategy.


Solution

  • if you do a findElement in Selenium it will give you the element that first appears on the page in the same logical order that a findElement would return if you did this in JavaScript.

    Obviously, there are multiple ways to do a lookup and the exact implementation is going to be dependant on the driver implementation (FireFox, Google, IE, etc) But I have always gotten what I would expect and not anything like the wacky TC.

    My answer is based on my own experience with the product. I can't find any exact authoritative source that would document this for you.