[英]How to get product reviews and then click <a> element of product with most reviews (Selenium, Java)
<section id="135156028" data-scrolled="false" class="AdItem_adOuterHolder__Z29Nf">
<article class="AdItem_adHolder__GL0yo">
<a class="Link_link__J4Qd8 Link_inherit___qXEP" target="_self" id="" rel="" role="button"
tabindex="-1" href=".." style="align-items: flex-start;">
<div class="AdItem_imageHolder__DYGd_" style="max-width: 128px; min-width: 128px; height:
120px;">
<img src="...">
</div>
</a>
<div class="AdItem_descriptionHolder__xnkD4">
<div class="AdItem_adInfoHolder___36KR"></div>
<div class="AdItem_price__k0rQn">650 din</div>
<div>
<div class="AdItem_viewAndFavorite__pjskf">
<div class="AdItem_favoriteHolder__bKBRE">
<svg width="16" height="16" viewBox="0 0 16 16" fill="none"
xmlns="http://www.w3.org/2000/svg" class="asIcon_greyStroke__NU1Xe
asIcon_svg__Zm34q">
</svg>
<span class="AdItem_count__iNDqG">**144**</span>
</div>
<div class="AdItem_favoriteHolder__bKBRE">
<svg width="16" height="16" viewBox="0 0 16 16" fill="none"
xmlns="http://www.w3.org/2000/svg" class="asIcon_greyStroke__NU1Xe
asIcon_svg__Zm34q">
</svg>
<span class="AdItem_count__iNDqG">13</span>
</div>
</div>
<div class="AdItem_postedStatus__C6vfK">...</div>
</div>
</div>
</article>
</section>
This is the code, I deleted some unnecessary thing, but in case I deleted some importat part of the code, link is here: [https://novi.kupujemprodajem.com/mama-i-beba/oprema-za-hranjenje-i-dojenje/grupa/18/2473/1].这是代码,我删除了一些不必要的东西,但如果我删除了代码的一些重要部分,链接在这里:[https://novi.kupujemprodajem.com/mama-i-beba/oprema-za-hranjenje- i-dojenje/grupa/18/2473/1]。 I took random product to share code, not the product with most views, but what I want to do is to get views count from here: "xpath = "//div[@class='AdItem_viewAndFavorite__pjskf']//div[1]", to check which product has highest number, and then I want to click on element of that product. I'm newbie, and I don't know how to connect those, and I didn't find any solutions on the inte.net.我拿了随机产品来分享代码,而不是浏览量最多的产品,但我想做的是从这里获取浏览量:"xpath = "//div[@class='AdItem_viewAndFavorite__pjskf']//div[1] “,检查哪个产品的数量最多,然后我想点击那个产品的元素。我是新手,我不知道如何连接这些,我在互联网上没有找到任何解决方案。网。
I tried something like this:我试过这样的事情:
@FindBy(xpath = "//div[@class='AdItem_name__BppRQ']")
List<WebElement> productsList;
@FindBy(xpath = "//div[@class='AdItem_viewAndFavorite__pjskf']//div[1]")
List<WebElement> productViewsList;
public SingleProductPage chooseHighestViewsProduct(){
String productName;
String productViews;
int numberOfViews;
HashMap<Integer,String> mapFinalProducts = new HashMap<Integer, String>();
for (int i=0;i<productsList.size();i++){
productName = productsList.get(i).getText();
productViews = productViewsList.get(i).getText();
numberOfViews = Integer.parseInt(productViews);
mapFinalProducts.put(numberOfViews, productName);
}
Set<Integer> allKeys = mapFinalProducts.keySet();
ArrayList<Integer> listOfProductValues = new ArrayList<Integer>(allKeys);
Collections.sort(listOfProductValues);
int highestViews = listOfProductValues.get(listOfProductValues.size()-1);
}
This way I get the product with most views, but because that span is not clickable, I don't know how to click on its link.通过这种方式我得到了浏览量最多的产品,但是因为那个 span 是不可点击的,所以我不知道如何点击它的链接。
Once you get the most views product then find preceding element of a
tag and then click.一旦获得最多的浏览量产品,然后找到标签的前a
元素,然后单击。
productViewsList.get(highestViews).findElement(By.xpath("./preceding::a[1]")).click()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.