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

Please help me to resolve 2 issues:

  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)
  2. How I to action on xpath with search text

Thanks so much

1 Like

could you add a delay before the element

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

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

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:

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

Thanks all for suggest and help me.