Validate element exist using Find()

Ask general questions here.
Posts: 15
Joined: Thu Nov 08, 2018 2:22 am

Validate element exist using Find()

Post by praneet » Mon Feb 25, 2019 11:14 am

Below is my dynamic code to verify that element present or not.
If element is not present then i want boolean return because based on boolean return i will do another operation.

Below is the code not working
var elementin = doucmentDiv.FindSingle("./table["+k+"]/tbody/tr/td[3]/div/a");
bool found = Validate.Exists(elementin);

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

Re: Validate element exist using Find()

Post by odklizec » Mon Feb 25, 2019 12:23 pm


And what exactly is your question? Validate.Exists returns true/false, so it does exactly what you want. In my opinion, FindSingle is not even required. In fact, it's counterproductive to use it in your code, because it will always throw an exception if element is not found. So you can easily use Validate.Exists(rxpath, searchTimeout, message, exceptionOnFail) and set exceptionOnFail as false. This will do exactly what you want.

BTW, I would suggest not to use rxpath directly in code and instead to pass the repoinfo element to code. Using rxpath directly in code makes the test suite hard to understand and maintain! Once your developers change something in SUT UI, you will have to find and fix the xpath in code, possibly at many places. Hence using central repository is most recommended (and clever) way of passing elements to code.
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