# \[Help me\] Add a string as text search and action on record that text in a xpath

**URL:** <https://forum.katalon.com/t/help-me-add-a-string-as-text-search-and-action-on-record-that-text-in-a-xpath/167207>\
**Category:** Web Testing\
**Tags:** katalon-studio\
**Created:** [March 5, 2025, 3:54am UTC](https://forum.katalon.com/t/help-me-add-a-string-as-text-search-and-action-on-record-that-text-in-a-xpath/167207 "2025-03-05T03:54:10Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![dung\_nguyen11](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/dung_nguyen11/32/30227_2.png) [@dung\_nguyen11](https://forum.katalon.com/u/dung_nguyen11)\
**Post date:** [March 5, 2025, 3:54am UTC](https://forum.katalon.com/t/help-me-add-a-string-as-text-search-and-action-on-record-that-text-in-a-xpath/167207/1 "2025-03-05T03:54:10Z")

</div>

Please help me to resolve 2 issues:

 ![image](https://europe1.discourse-cdn.com/katalon/original/3X/0/4/04325152be11de48ec45742fc8d926aa86dbf7e1.png)

1. When I run a script above, I have a problem in console related **stale-element-reference-exception** \>\> I try to copy a code in try{} and paste on catch{} block with refreshBrowser() but it’s not working ( [Understanding Stale Element Reference Exception in Selenium | BrowserStack](https://www.browserstack.com/guide/stale-element-reference-exception-selenium))
2. How I to action on xpath with search text  
 ![image](https://europe1.discourse-cdn.com/katalon/original/3X/8/2/82704926102c3da7649d9a57a32836cb064d2cc2.png)

Thanks so much

---

<div class="post-metadata">

**Author:** ![Monty\_Bagati](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/monty_bagati/32/33047_2.png) [@Monty\_Bagati](https://forum.katalon.com/u/Monty_Bagati)\
**Post date:** [March 5, 2025, 3:56am UTC](https://forum.katalon.com/t/help-me-add-a-string-as-text-search-and-action-on-record-that-text-in-a-xpath/167207/2 "2025-03-05T03:56:12Z")

</div>

could you add a delay before the element

---

<div class="post-metadata">

**Author:** ![dung\_nguyen11](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/dung_nguyen11/32/30227_2.png) [@dung\_nguyen11](https://forum.katalon.com/u/dung_nguyen11)\
**Post date:** [March 5, 2025, 12:01pm UTC](https://forum.katalon.com/t/help-me-add-a-string-as-text-search-and-action-on-record-that-text-in-a-xpath/167207/3 "2025-03-05T12:01:13Z")

</div>

I have tried delay but it’s sometimes running passed, sometimes failed. I also paste code in catch() block but almost are failed. By the way, related issue 2: add variable into xpath effectively

---

<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:** [March 5, 2025, 1:58pm UTC](https://forum.katalon.com/t/help-me-add-a-string-as-text-search-and-action-on-record-that-text-in-a-xpath/167207/4 "2025-03-05T13:58:29Z")

</div>

I’ve never used “By.partialLinkText” or “By.find”, however, what is with the downslash (i.e. \ ) before your “${s2}”? How about just try?

```auto
WebElement teObj = driver.findElement(By.xpath("//*[text()='${s2}']/ancestor::mat-cell"))

```

As long as you start (and end) your pathway with double quotes, the pathway should take your parameterized statement.  
The other thing I notice is that you have double slash before “ancestor”. That is not necessary as “ancestor” is going **up** the nodes, not down. (Use double slashes for going down several nodes.) Try it as a single slash as I have done above, see if that helps.

As for your “staleElement”, I agree with Monty that you probably need a delay within your loop, but you also might need to “re-read” your **elements** that you get before your “for loop” **within** the “for loop” at the end. That sometimes works.

Edit:  
I just started thinking on the “partialLinkText” and if the text you need to use is only part of a whole, then you might use:

```auto
WebElement teObj = driver.findElement(By.xpath("//*[contains(text(),'${s2}')]/ancestor::mat-cell"))

```

---

<div class="post-metadata">

**Author:** ![dung\_nguyen11](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/dung_nguyen11/32/30227_2.png) [@dung\_nguyen11](https://forum.katalon.com/u/dung_nguyen11)\
**Post date:** [March 10, 2025, 2:23am UTC](https://forum.katalon.com/t/help-me-add-a-string-as-text-search-and-action-on-record-that-text-in-a-xpath/167207/5 "2025-03-10T02:23:51Z")

</div>

Thanks all for suggest and help me.
