Dynamic selectors based on element text

Hello all,

I would like to know if you come across situations, when selectors are created dynamically based on text and how do you approach solving it. For example:

User can place many different items from a web store in the cart. And each item in the cart has same base selector:

//div[@class="cart_item" and text()="${item_title}"]/div[@class="item-description"]

Here ${item_title} will be dynamic, depending on which items were added to the cart. It does not make sense to create a selector for every item in web store, since there are too many and the only difference between their selectors is the item title.

Is there an elegant way to solve this easily and make a single selector reusable for any item title?

Thank you and best regards,

Could just get all cart item elements then you can do action depending on your needs against the list of elements.

You’d probably could just make a few very specific ‘action keywords’ for your cart/cart robot file, then use those actions against your list element items.

I would like the action to affect a single specific element, or few of them. Not all of them, at least in most cases.

Yeah so you’d control what items in the list of elements you’d want to do said action against when you loop over.

If there’s multiple items but not all just make a list of those you’d want to do action against, loop over all elements and get the text, if it exist ’in’ your list of titles then do action against it.

You could still continue to pass a variable in to the locator or in the above when it ‘exists’ just use that element.

If you expect it to exist but doesn’t then handle it so the remaining can continue.

This is just one way of doing it based on what’s provided.

Thanks :slight_smile:

1 Like

Can’t you make a keyword something like ‘Add item to cart’ with a parameter which contains the item title, and in this keyword you use the base selector?

1 Like

Thank you for your suggestions. Creating a keyword that fills the parameter on the spot is currently the way to go. I try to keep variables/selectors in separate resources from keywords, so they can be as high level as possible. Regardless of that though it works okay.