Java - Selenium - Use contains with element list

Asked

Viewed 1,212 times

0

I have on a website the products that were added in the cart. I want to check that the product "Test T-Shirt" has been successfully added. For this, I have a string where this value (Test T-Shirt) is set. Ex: String _product1 = "Camiseta Teste".

However, the products added in the cart have the same id (name). In this ID, when I use a gettext(); the name of each product is returned.

I want to do the following:

Check whether the string _product1 is equal to the text of any of these id name products.

So my code needs to get this list of elements (name). Check the first product of the cart, see what it contains, if it is equal to the _product1 variable, if it is, pass the test. If not, continue to the last existing element of the list. If any of these elements is equal to _product1, ok. Otherwise, the test fails.

  • I don’t quite understand, but I think you can do something like: for(Objeto o : lista)

  • Then check out: if(o.getVariavel().contains("abc"){//faz algo}

  • @Techies rephrased the question again, if you can still help me. Thank you very much!

  • Is there a way to put the HTML part that contains the products? I think the best approach in this case is not to use id element, but return all child elementros, either using a CSS class in the parent element, xpath, etc..

  • Easier https://www.netshoes.com.br/account/wish-list.jsp If you choose any product and add it to the cart list, you can find it by cssSelector '.name' and pick up the text from its description. Only this css is the same for all added products. I need to check one by one and check if the text of any of them matches the initial string @Brunocésar

  • I think the piece of code I posted solves your problem.

  • @Luiísgustavovieira Is this only in the wish list? Why in the normal cart is a table with class table-products, So it’s pretty simple to do. If it’s just on the wish list I can’t test, I won’t register just for it, so if I can include the structure it’s simpler.

Show 2 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.