Using XPath to identify an element not containing something?

Ask general questions here.
Fergal
Certified Professional
Certified Professional
Posts: 298
Joined: Tue Feb 18, 2014 2:14 pm
Location: Co Louth, Ireland
Contact:

Using XPath to identify an element not containing something?

Post by Fergal » Thu Jan 18, 2018 12:42 pm

I am testing an application that contains a number of buttons. Some of those buttons contain a text element with an index of 4 and a text value of
"(Voided)". Using an XPath that ends with "/button[@automationid='button']/text[index()=4 and @text='(Voided)']" finds the buttons that contain a text element with an index of 4 and a text value of "(Voided)".

How can that XPath be changed to find the first button which does not contain a text element with an index of 4 and a text value of
"(Voided)"?

Thanks.

User avatar
odklizec
Ranorex Guru
Ranorex Guru
Posts: 3930
Joined: Mon Aug 13, 2012 9:54 am
Location: Zilina, Slovakia

Re: Using XPath to identify an element not containing something?

Post by odklizec » Thu Jan 18, 2018 12:57 pm

Hi,

Well, the obvious solution looks like this:

Code: Select all

/button[@automationid='button']/text[index()!=4 and @text!='(Voided)']
But I guess you mean to find a button, which actualy does not contain the Text sub-element? In this case,
I'm afraid, there is no xpath way to detect this. I've already discussed a similar issue with Ranorex folks and there is no xpath way to detect a non-existence of an element. I think the only solution is using Find method?
Last edited by odklizec on Thu Jan 18, 2018 1:13 pm, edited 1 time in total.
Pavel Kudrys
Ranorex explorer at Descartes Systems

Please add these details to your questions:
  • Ranorex Snapshot. Learn how to create one >here<
  • Ranorex xPath of problematic element(s)
  • Ranorex version
  • OS version
  • HW configuration

Fergal
Certified Professional
Certified Professional
Posts: 298
Joined: Tue Feb 18, 2014 2:14 pm
Location: Co Louth, Ireland
Contact:

Re: Using XPath to identify an element not containing something?

Post by Fergal » Thu Jan 18, 2018 1:07 pm

Thanks for your helpful reply odklizec.
odklizec wrote:...I guess you mean to find a button, which actual does not contain the Text sub-element?..
Yes, that's exactly what I want to do.
odklizec wrote:...I think the only solution is using Find method?
Thanks, are you referring to writing some code to identify the element?

User avatar
odklizec
Ranorex Guru
Ranorex Guru
Posts: 3930
Joined: Mon Aug 13, 2012 9:54 am
Location: Zilina, Slovakia

Re: Using XPath to identify an element not containing something?

Post by odklizec » Thu Jan 18, 2018 1:15 pm

Fergal wrote:Thanks for your helpful reply odklizec.
odklizec wrote:...I think the only solution is using Find method?
Thanks, are you referring to writing some code to identify the element?
Yes, you will have to write some code to identify a button without requested sub-elements.
Pavel Kudrys
Ranorex explorer at Descartes Systems

Please add these details to your questions:
  • Ranorex Snapshot. Learn how to create one >here<
  • Ranorex xPath of problematic element(s)
  • Ranorex version
  • OS version
  • HW configuration

Fergal
Certified Professional
Certified Professional
Posts: 298
Joined: Tue Feb 18, 2014 2:14 pm
Location: Co Louth, Ireland
Contact:

Re: Using XPath to identify an element not containing something?

Post by Fergal » Thu Jan 18, 2018 1:27 pm

odklizec wrote:there is no xpath way to detect a non-existence of an element
Thanks again odklizec for steering me in the right direction. Clarifying that helped me to stop focusing on that approach and figure out a way that works.

I've discovered that "Voided" text element exists in all of the buttons, but is invisible in the button required. The updated XPath, which works by finding the parent element (i.e. the required button) of the first invisible text element, ends with the following:

Code: Select all

...text[@name=' (Voided)' and @visible='False'][1]/..

User avatar
odklizec
Ranorex Guru
Ranorex Guru
Posts: 3930
Joined: Mon Aug 13, 2012 9:54 am
Location: Zilina, Slovakia

Re: Using XPath to identify an element not containing something?

Post by odklizec » Thu Jan 18, 2018 1:30 pm

Of course, if the child element is available, just not visible, your solution should be just fine. In my case, sub-elements are sometimes not available at all ;)
Pavel Kudrys
Ranorex explorer at Descartes Systems

Please add these details to your questions:
  • Ranorex Snapshot. Learn how to create one >here<
  • Ranorex xPath of problematic element(s)
  • Ranorex version
  • OS version
  • HW configuration