# Could not found Angular Element by regex

**URL:** <https://forum.katalon.com/t/could-not-found-angular-element-by-regex/49596>\
**Category:** Web Testing\
**Tags:** katalon-studio\
**Created:** [November 25, 2020, 4:52pm UTC](https://forum.katalon.com/t/could-not-found-angular-element-by-regex/49596 "2020-11-25T16:52:21Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![simon.klotz](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/simon.klotz/32/16104_2.png) [@simon.klotz](https://forum.katalon.com/u/simon.klotz)\
**Post date:** [November 25, 2020, 4:52pm UTC](https://forum.katalon.com/t/could-not-found-angular-element-by-regex/49596/1 "2020-11-25T16:52:21Z")

</div>

Hello friends 🙂

I have looked up many topics over here about regex and similar stuff but still no solution.

So here my problem:  
I’m testing an angular web-application with katalon right now, and there is one Element that makes me freaking out:  
If I right click on a radio-button and open the so called context-menu I have 4 options, as you can see in the picture below:

 ![KontextMenu](https://europe1.discourse-cdn.com/katalon/original/2X/5/532d0e7616c028795c6b69106af1147176cd4189.png)

Everytime I want Katalon to click on the 4. option it could not find it, cause the elements are newly generated each time. (The options 1 - 3 are working each time some how, and i don’t now why)  
I saved the object several times with Object Spy.

One time the xpath looked like this:

```
//div[@id='mat-menu-panel-1']/div/div/a[4]

```

and another time:

```
//div[@id='mat-menu-panel-3']/div/div/a[4]

```

So the id is not everytime the same.  
As I found some topics over here with a similar problem, I got the idea of using regex.  
I tried to match the xpath with the following regex:

```
//div\[@id='mat-menu-panel-(\d)']/div/div/a\[4]

```

 ![ObjectProperties](https://europe1.discourse-cdn.com/katalon/original/2X/5/5ff94aa0c0fa8ec660ec63ad9d2766db6ae18cb2.png)

Looks good so far: [https://regex101.com/r/Nzx8Kl/13](https://regex101.com/r/Nzx8Kl/13)

But Katalon is still not able to find the element.

Or is Katalon just still not able to handle Angular?  
(as I read in other topics there have been difficulties)

Hope to get some help,

thank you!  
Simon

---

<div class="post-metadata">

**Author:** ![grylion54](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/grylion54/32/16265_2.png) [@grylion54](https://forum.katalon.com/u/grylion54)\
**Post date:** [November 25, 2020, 7:03pm UTC](https://forum.katalon.com/t/could-not-found-angular-element-by-regex/49596/2 "2020-11-25T19:03:06Z")

</div>

Personally, I think you are almost there. I don’t use Angular but I do use RegEx occasionally. **And** may I suggest to escape the opposing square bracket like you did the first, like:

`id('mat-menu-panel-\\d')/div/div/a\\[4\\]`

Note: I also used another form of xpath since you have an **id** and it gets rid of one set of square brackets. Also, I’m not sure about escaping the **back-slashes** in the pathway.

---

<div class="post-metadata">

**Author:** ![kazurayam](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/kazurayam/32/333_2.png) [@kazurayam](https://forum.katalon.com/u/kazurayam)\
**Post date:** [November 26, 2020, 12:57am UTC](https://forum.katalon.com/t/could-not-found-angular-element-by-regex/49596/3 "2020-11-26T00:57:12Z")

</div>

> [@simon.klotz](#):
>
> As I found some topics over here with a similar problem, I got the idea of using regex.  
> I tried to match the xpath with the following regex:

This would never work. You can not use Regular Expression in this way. You should forget about Regexp here.

---

<div class="post-metadata">

**Author:** ![kazurayam](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/kazurayam/32/333_2.png) [@kazurayam](https://forum.katalon.com/u/kazurayam)\
**Post date:** [November 26, 2020, 12:59am UTC](https://forum.katalon.com/t/could-not-found-angular-element-by-regex/49596/4 "2020-11-26T00:59:32Z")

</div>

I would suggest you to try the following XPath expression:

`//div[starts-with(@id, 'mat-menu-panel-')]/div/div/a[4]`

See  
[https://www.guru99.com/xpath-selenium.html#8](https://www.guru99.com/xpath-selenium.html#8)  
to learn more about XPath.

---

<div class="post-metadata">

**Author:** ![simon.klotz](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/simon.klotz/32/16104_2.png) [@simon.klotz](https://forum.katalon.com/u/simon.klotz)\
**Post date:** [November 27, 2020, 9:41am UTC](https://forum.katalon.com/t/could-not-found-angular-element-by-regex/49596/5 "2020-11-27T09:41:31Z")

</div>

Hi thank you both for your help!  
The xpath I got from you kazurayam worked! Awesome!!

But just because I’m interested:

> [@kazurayam](#):
>
> This would never work. You can not use Regular Expression in this way. You should forget about Regexp here.

Why?  
I mean the option is called “matches regex”… so this is not quite intuitive, if regular expressions actually wont (never?) work.

---

<div class="post-metadata">

**Author:** ![kazurayam](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/kazurayam/32/333_2.png) [@kazurayam](https://forum.katalon.com/u/kazurayam)\
**Post date:** [November 27, 2020, 12:56pm UTC](https://forum.katalon.com/t/could-not-found-angular-element-by-regex/49596/7 "2020-11-27T12:56:15Z")

</div>

> [@simon.klotz](#):
>
> Why?  
> I mean the option is called “matches regex”…

Up until now, I wasn’t aware that Katalon actually proposes an option “match regex” with value “//div[@id=‘mat-menu-panel-(\d)’]/div/div/a[4]”. I was surprised to find a regex-like notation `(\d)`.

I haven’t seen it before. It seems that `(\d)` was recently added. I haven’t ever studied it. I think, this “Regex-flavored locator” syntax is something new, and not officially documented/announced at all.

I am afraid, it could be buggy. I have an opinion that this would NEVER work, though I would not bother describing it here. If you are interested, pls have a look at my discussion at [another post](http://forum.katalon.com/t/test-object-with-xpath-selector-using-matches-function-never-works/11822).

At least I would not use “match regex” as I know how to write effective XPath expressions without using regex-like things. For example, see another topic [TestObject that match IP Address-like string](http://forum.katalon.com/t/testobject-that-match-ip-address-like-string/45677)

---

<div class="post-metadata">

**Author:** ![kazurayam](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/kazurayam/32/333_2.png) [@kazurayam](https://forum.katalon.com/u/kazurayam)\
**Post date:** [November 27, 2020, 2:20pm UTC](https://forum.katalon.com/t/could-not-found-angular-element-by-regex/49596/8 "2020-11-27T14:20:05Z")

</div>

@ThanhTo

“match regex” as Regex-powered XPath-like locator — is it officially released already? It looks experimental to me.
