Friday, 11 October 2013

What is parentNode, childNodes, firstChild, lastChild in QTP?

Well - let me tell you that  all the properties (parentNode, childNodes, firstChild, lastChild) are not defined by QTP.

All these properties are HTML DOM properties that help us get the child and parent element of the given element in web page.

You can access these method in QTP using .object property which lets you access native methods and properties of the objects/elements.

Example - 

<body>
<div id="parentdiv">
 This text is inside div element of which id is - parentdiv

<div id="fc">
 This is nested div1
</div>


<div id="sc">
 This is nested div2.
</div>


</div>
</body>

As you can see in above example, 2 div elements ( fc, sc) are inside the div(parentdiv).
So for fc-div and sc-div, parentdiv is the parent element. While for parentdiv, fc-div and sc-div are child div elements.

You can get access to these elements using HTML DOM properties parentNode, childNodes, firstChild, lastChild.

For example -
'access the child objects
set divParentObject= Browser("myb").Page("myp").object.getElementById("parentdiv")
print divParentObject.firstChild.innerText
print divParentObject.lastChild.innerText

set allchilds = divParentObject.childNodes
for i=0 to allchilds.length-1
 print allchilds(i).innerText
next

'access the parent element of div with id "fc"
set childObject = Browser("myb").Page("myp").object.getElementById("fc")

print childObject.parentNode.innerText 

This is how we can access the elements using parentNode, childNodes, firstChild, lastChild in QTP.

  1. http://qtp-interview-questions.blogspot.in/2013/06/how-to-use-html-dom-in-qtp.html
  2. http://qtp-interview-questions.blogspot.in/2013/10/what-is-previoussibling-nextsibling-in.html
  3. http://qtp-interview-questions.blogspot.in/2013/10/what-is-parentnode-childnodes.html
  4. http://qtp-interview-questions.blogspot.in/2013/10/what-is-getelementsbytagname-in-qtp.html
  5. http://qtp-interview-questions.blogspot.in/2013/10/how-to-use-getelementbyid-in-qtp.html
  6. http://qtp-interview-questions.blogspot.in/2013/10/how-to-get-innertext-of-webelement-in.html

Please give your inputs, suggestions, feedback to Us about above QTP topic. We value your thoughts.
If you have any queries, you can drop the comment below. 

No comments:

Post a Comment

Please Leave your reply. We value your feedback and inputs

Best QTP Books

Everything About QTP

Hello Friends,
You can find QTP study material, Multiple choice questions (mcq), QTP question bank, QTP question papers, QTP notes, QTP questionnaire, scenario based QTP interview questions, QTP tutorial and QTP training on this site.

If you are a fresher or experienced QTP professional with (1/2/3/4) years of experience, this blog is just for you.