0

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.

Makjb lh
  • 432
  • 5
  • 16

1 Answers1

1

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.

Dave Bush
  • 2,382
  • 15
  • 12
  • I would'nt say the TC is wacky. It's just that Javascript implementation and TC would list out in a certain order. And by the way they are using JScript - microsoft javascript implementation. Secondly I am not able to understand if you answered for traversing order of tags. its very important I find it because if there is some vertical search, we can narrow it down faster using xpath. I am trying that though – Makjb lh Feb 22 '16 at 16:59
  • My experience is that it is in the order the HTML list out. Depth first search. XPath, if you can do it, may be faster by milliseconds. Not something I would worry about other than specifying exactly which element you are looking for. – Dave Bush Feb 22 '16 at 17:01
  • Lemme try to verify that. I need to probably create a custom Html for that – Makjb lh Feb 22 '16 at 17:03
  • Any hints suggestion - what exact javascript command Selenium uses for searching ? – Makjb lh Feb 22 '16 at 17:31
  • Like I said, that is going to depend on the driver implementation. – Dave Bush Feb 22 '16 at 17:37