Ask a Question

Using regexp for FindElement

SOLVED
enriquebravo
Contributor

Using regexp for FindElement

Hello,

 

I read several other forum post similar to my issue and seems like regexp and FindElement may not work.

 

I got the following dynamic xpath: FindElement("//div[@id='__49_0']//input") Unfortunately, this is the only selector detected by TestComplete to identify the textbox. The dynamic part of the selector are the numbers.

 

I was thinking of using a regex and came up with the expression: Page.FindElement("regexp:\/\/div\[\@id='__\d+_\d+']\/\/input')") I am using JavaScript so I may be missing some backslashes to escape characters, not sure. 

 

I am using TestComplete 15, and seems like when using the Find methods, only xpath 1.0 is supported so the xpath functions to recognize a strings is limited.

 

By the way, when the FindElement method is successful, does it automatically click on the object?

 

Any suggestions? 

 

Regards.

 

I am using TestComplete 15 and seems like only xpath 1.0 is supported when used with the Find methods. Am I right on that?

 

6 REPLIES 6
npaisley
Staff

Hey @enriquebravo!

 

Verifying the supported Xpath version will take some research, but if ultimately you are just looking for a way to handle the dynamic portion of your mapping so it can continue to be found in subsequent tests, we can use Wildcards for this.

 

For instance, we can Wildcard the dynamic portion of your mapping by using an asterisk (*). 

Orginal mapping = //div[@id='__49_0']//input

Wildcard mapping = //div[@id='__*']//input

 

Just in case this is helpful and you are unfamiliar, the below doc covers the usage of Wildcards in TestComplete.

https://support.smartbear.com/testcomplete/docs/reference/misc/using-wildcards.html


Regards,
Nick

Solutions Engineer @ SmartBear
AlexKaras
Community Hero

Hi,

 

Documentation for .FindElement() does not mention possibility to use regular expressions, so I am far not sure that it will work. Another consideration to support this thought is that .FindElement() was introduced with cross-platform web testing and seems to transfer all calls to Appium under the hood. Thus, as Appium does not support regexp for XPath/CSS selectors, likewise .FindElement() does not.

 

when using the Find methods, only xpath 1.0 is supported 

Correct. This is documented in the article for FindElement.

 

when the FindElement method is successful, does it automatically click on the object?

No. Just a found element is returned and it is up to your test code to do what is required with the found element (click it, drag it, use it as an anchor for the next search, etc.).

 

Regards,
  /Alex [Community Hero]
____
[Community Heroes] are not employed by SmartBear Software but
are just volunteers who have some experience with the tools by SmartBear Software
and a desire to help others. Posts made by [Community Heroes]
may differ from the official policies of SmartBear Software and should be treated
as the own private opinion of their authors and under no circumstances as an
official answer from SmartBear Software.
The [Community Hero] signature is used with permission by SmartBear Software.
https://community.smartbear.com/t5/custom/page/page-id/hall-of-fame
================================

Hey Nick,

 

Thanks for the suggestion. Unfortunately, there was no luck. Using the wildcard caused an object not found error message. I ended up using a SendKeys instruction to focus on the expected field. 

I am focusing on using keyword so maybe there is an alternate way to handle the issue over code, but right now I don't have much more time.

 

Thanks for the help!

Hey Alex,

 

So my understanding is that using regexp is not much of an option when specifying selector values. I guess it is a drawback of using xpaths or css selectors instead of the good-old object properties. I had a similar issue with a Logout link, which I would think is easy to identify but it wasn't the case. There is a dependency on the parent object page. I was trying to find a way how to make it "page-independent", but I couldn't as the object relies on xpath.

 

Thanks for your note and for looking into it. I ended up using SendKeys and it works ok. Will see if there are other options in the future.

 

Regards,

chriscc
Contributor

Hi @enriquebravo,

 

I am not a xpath expert...if the id number is dynamic, and id isn't unique to that div element, then you shouldn't have to look for the id attribute in your expression.  Something like  this might work: FindElement("//div//input")

 

if the id is unique to that div element then you can simply verify that it exists, something like this might work:  FindElement("//div[@id]//input")

 

if part of the attribute is unique, like the underscores, then you might be able to use the contains method, like this:  FindElement("//div[contains(@id, '__')]//input")

 

Not sure if any of this works or helps, but I thought I'd share.  

Chris
enriquebravo
Contributor

Thanks! The third option worked! Putting these xpath expressions together is tricky. TC now interacts with the screen control without using sendkeys 🙂 

 

Have a great day!

cancel
Showing results for 
Search instead for 
Did you mean: