1 year ago
#285308
TANYA
java, selenium,selenide: adding random product to the cart (stream)
i am using (java, selenium,selenide). there is a list of product div on a page. it contains product information, price and buy button. i need to randomly select 2 products that have price < 290 and click on buy button in the corresponding div. help please. it is important.
private final String parent = "//div[@class ='catalog-item']"; //div-ы
private final String childCost = "/div[@class='catalog-item-store']/div[@class='catalog-item-price']";//переход от div к цене
public void randomSelectionElement() {
// ArrayList upto290 = new ArrayList<>();
List listCardsGoods = $$x(parent);//лист дивов
SelenideElement cost = $x(parent + childCost); //перешли к элементу Цена
ElementsCollection costList = $$x(parent + childCost);//коллекция цен
List sortCost = costList.stream().filter(SelenideElement -> Integer.valueOf(cost.getText().replace(" руб", "").replace(",", ".")) < 290).);
Random rand = new Random();
for (int i = 0; i < 1; i++) {
int randomIndex = rand.nextInt(sortCost.size());//выбираем рандомный индекс
sortCost.get(randomIndex);//берем элемент
//взять xpath этой цены и перейти к его кнопке Купить
sortCost.remove(randomIndex);
}
selenide
0 Answers
Your Answer