Click on Text found with XPATH

I have a product listing page with 1-n products there. Unfortunately the source is not really automation friendly, means, all products on the listing are equal regarding html attributes. The only way they differ are the prices and of course the product names.

The source looks like this (abstract):

div class=“product-item-details”>

a href=“https://testurl.html” class=“product-item-link”>
h3 class=“product-item-name”>MYPRODUCTNAME

span class=“price-container price-final_price tax weee”>
span id=“product-price-2293” data-price-amount=“12345” data-price-type=“finalPrice” class="price-wrapper
">
span class=“price”>CHF 12’345.00
/div>

next product …

I tried a lot, but never succeeded in finding the product on the page matching product name = MYPRODUCTNAME, and then to click on the name so that I get redirected to the product detail page.

Any help is very appreciated folks.

Thanks

Hi,

what about

//h3[contains(text(), 'MYPRODUCTNAME')]

Given that product name is always unique substring, this may work.

That looks good - BUT

How to do that concrete in Katalon? Because the search string can vary - means - I use a variable for it. And I have no idea how to add a variable into an object repository element?

Could you provide me a link to a snippet - that would enormous help me.

1 Like

Probably the simplest way would be new custom keyword which will handle those objects. Look:

public static TestObject getTestObjectForMyProduct(String productName) {
	return new TestObject().addProperty("xpath", ConditionType.EQUALS, "//h3[contains(text(), '" + productName + "')]")
}

How to use it?

WebUI.click(MyCustomKeyword.getTestObjectForMyProduct("MyProductName"))

Just pass product name to the method and it will return TestObject which identifies the product you want.

3 Likes

It works - Thank you - that helped a lot!

Hii, I also happen want to try this method but the text element was not found

the error in log viewer

Unable to find the element located by ‘By.xpath: //h3[contains(text(), ‘1846’)]’. Please recheck the objects properties to make sure the desired element is located.

did I missing something?

Your XPath is invalid. Make sure that such an element exists.

It search xpath address that contains the value, is that right?
also is there any method to click an object based on text that displayed in html with random generated xpath? (Click an id number, the number in html as a pointer)

here is the xpath list