Relative xpaths are failing to find any screen elements, leading to this error:
"System.NullReferenceException: Object reference not set to an instance of an object."
This problem occurs in a coded step, following this kind of pattern:
The first line, where it calls the Find method on the active browser and uses an absolute xpath, works fine. But the second line, calling the Find method on the particular HtmlControl and using a relative xpath to find another element, fails; the variable element2 remains null, and the further coded steps fail with the error above.
I've attached a complete project which uses a very simple test to demonstrate this problem.
"System.NullReferenceException: Object reference not set to an instance of an object."
This problem occurs in a coded step, following this kind of pattern:
HtmlControl element1 = Manager.ActiveBrowser.Find.ByXPath<HtmlControl>(
"//div[@class='sw_b']"
);
HtmlControl element2 = element1.Find.ByXPath<HtmlControl>(
"./input[@id='sb_form_q']"
);
The first line, where it calls the Find method on the active browser and uses an absolute xpath, works fine. But the second line, calling the Find method on the particular HtmlControl and using a relative xpath to find another element, fails; the variable element2 remains null, and the further coded steps fail with the error above.
I've attached a complete project which uses a very simple test to demonstrate this problem.
8 Answers, 1 is accepted
0
Hi Vince,
You just need a small change in the code and it will work as expected:
Just replace ./ with // That way you will find any elements that are relative to the first element.
You can read more about Xpath Syntax in this article.
// - Selects nodes in the document from the current node that match the selection no matter where they are.
I hope this helps.
Regards,
Velin Koychev
Telerik
You just need a small change in the code and it will work as expected:
HtmlControl element1 = Manager.ActiveBrowser.Find.ByXPath<
HtmlControl
>("//div[@class='sw_b']");
HtmlControl element2 = element1.Find.ByXPath<
HtmlControl
>("//input[@id='sb_form_q']");
Just replace ./ with // That way you will find any elements that are relative to the first element.
You can read more about Xpath Syntax in this article.
// - Selects nodes in the document from the current node that match the selection no matter where they are.
I hope this helps.
Regards,
Velin Koychev
Telerik
0
Vince
Top achievements
Rank 1
answered on 08 Aug 2013, 04:15 PM
Thank you Velin, that stops the error occurring.
However, there is a difference between ./ and // notation, so if test studio only supports // notation it is a limitation.
// means to identify any descendant of the current context, while ./ means to identify any child of the current context.
So for example in this sample of html:
If div 1 is the current node, then the following relative xpaths should return different results:
//div should return div 2, div 3 and div4.
but
./div should return only div 2.
Is there any reason why the ./ notation is not supported by telerik?
However, there is a difference between ./ and // notation, so if test studio only supports // notation it is a limitation.
// means to identify any descendant of the current context, while ./ means to identify any child of the current context.
So for example in this sample of html:
<
div
name
=
"1"
>
<
div
name
=
"2"
>
<
div
name
=
"3"
>
<
div
name
=
"4"
>
If div 1 is the current node, then the following relative xpaths should return different results:
//div should return div 2, div 3 and div4.
but
./div should return only div 2.
Is there any reason why the ./ notation is not supported by telerik?
0
Bob
Top achievements
Rank 1
answered on 08 Aug 2013, 04:28 PM
I believe Test Studio only returns the first result because it only works with a single element at a time. Therefore, whether it returns 2,3,4 or 2 makes no difference.
0
Vince
Top achievements
Rank 1
answered on 09 Aug 2013, 08:30 AM
Actually Robert, it's often necessary to work with multiple elements - that's exactly what the Find.AllByXpath method (and similar methods) are designed for.
0
Bob
Top achievements
Rank 1
answered on 09 Aug 2013, 12:36 PM
Time permitting, what are some ways you use this? I'm hoping there are ways I can implement it in my own testing.
I read the following to try and get up to speed on all the element identification things I could. I just can't think of a way to use most of them.
http://www.telerik.com/automated-testing-tools/support/documentation/user-guide/write-tests-in-code/intermediate-topics/element-identification/finding-page-elements.aspx
I read the following to try and get up to speed on all the element identification things I could. I just can't think of a way to use most of them.
http://www.telerik.com/automated-testing-tools/support/documentation/user-guide/write-tests-in-code/intermediate-topics/element-identification/finding-page-elements.aspx
0
Hi Robert,
I spent some time investigating the issue with the XPath expression and you are absolutely right that it is not working as expected.
Velin Koychev
Telerik
I spent some time investigating the issue with the XPath expression and you are absolutely right that it is not working as expected.
I filed a bug report on this problem, which you can follow in our Feedback Portal. I am sorry for any inconvenience it has probably caused and have updated your Telerik Points accordingly.
Let me know if you need assistance on writing the right Find Expression for some element.
Thank you for your understanding.
Velin Koychev
Telerik
0
Bob
Top achievements
Rank 1
answered on 13 Aug 2013, 12:57 PM
Would love to take credit, but I think Vince here was the one to report it.
0
Hello Robert,
You are absolutely right, my mistake. It seems that I actually gave the points correctly to Vince, so we have justice.
Regards,
Velin Koychev
Telerik
You are absolutely right, my mistake. It seems that I actually gave the points correctly to Vince, so we have justice.
Regards,
Velin Koychev
Telerik