Xpath Subtring help

Sorry if wrong forum. I’m new. I want to modify this xpath so that it only get’s the row that ends with “170”. Right now it gets both because they both contain “170” but I can’t figure out the substring to do what I want.


By rowBy = By.xpath(“//tr[contains(.,'” + billerName.toUpperCase() + “‘)][contains(.,’” + customerAccount.substring(customerAccount.length() - 3) + “')]”);

Thanks for any help!

Possibly you want “ends-with(str1, str2)” function in your xpath.

Unfortunaltely the XPath 1.0 spec does not support “ends-with(str1, str2)”. All browsers only supports XPath 1.0, no XPath 2.0 implementation is available in browsers.

However you can write an equivalent XPath expression using substring() and string-length() function of XPath1.0, see the following post for more info:

You tagged this with “AI”. Why? I suppose AI wouldn’t help you much here.

Or, why not you ask the same question to ChatBot?

I would be interested very much if you disclose your conversation with ChatBot here.

1 Like

Are you saying that your table has 777922170 rows, or that somewhere on a row, there is the value 777922170? If you can show some HTML of your issue, we may be able to get you a xpath to your cell.

Sorry I didn’t notice I did that. That was wrong, my bad.

I figured out the syntax finally! This was just my noob self not being very good at xpath. Here was the solution to get only the last three digits.

And yeah it sucks you can’t use ends-with because it’s not supported in chrome.

	By rowBy = By.xpath("//tr[contains(.,'" + billerName.toUpperCase() + "')]//td[contains(substring(.,string-length(.)-2,3),'" +  customerAccount.substring(customerAccount.length() - 3) + "')]");

Side note: you can format your strings declared with the double quotes with ${variableOrFunctionCall} for readability/maintainability

Another side note, hit up this resource for a guide on xpath

Great thanks. I’m such a bad coder lol. I knew about that but forgot about it. Thanks! And I’ll check that resource too.