简体   繁体   English

如何获取所有元素,即使它们具有相同的数据

[英]How to get all elements even if they have the same data

I have a project where I would like to gather in all brandname and price from a site.我有一个项目,我想从一个网站收集所有品牌名称价格 for that reason I have the following code:出于这个原因,我有以下代码:

List<WebElement> list_of_products = driver.findElements(By.xpath(loc.getProperty("brandName")));
    List<WebElement> list_of_products_price = driver.findElements(By.xpath(loc.getProperty("finalPrice")));


    //System.out.println("ezek a termékek"+ list_of_products);
    // Use of HashMaop to store Products and Their prices(after conversion to
    // Integer)
    String product_name;
    String product_price;
    int int_product_price;
    HashMap<Integer, String> map_final_products = new HashMap<>();
    {

        for (int i = 0; i < list_of_products.size(); i++) {

            product_name = list_of_products.get(i).getText();// Iterate and fetch product name
            product_price = list_of_products_price.get(i).getText();// Iterate and fetch product price
            product_price = product_price.replaceAll("[^0-9]", "");// Replace anything will space other than numbers
            int_product_price = Integer.parseInt(product_price);// Convert to Integer
            map_final_products.put(int_product_price, product_name);// Add product and price in HashMap
        }

        // write out all the products we found
         Reporter.log("All the prices and products we found: " +
         map_final_products.toString()+ "\n", true);

        // Get all the keys from Hash Map
        Set<Integer> allkeys = map_final_products.keySet();
        ArrayList<Integer> array_list_values_product_prices = new ArrayList<Integer>(allkeys);

        // this will sort in ascending order lowest at the top and highest at the bottom
        Collections.sort(array_list_values_product_prices);

As it is on console: XYs are the actual brandnames正如在控制台上一样: XY 是实际的品牌名称

All the prices and products we found: {20720=XY, 11490=XY, 13490=XY, 15490=XY, 19490=XY, 21990=XY, 16490=XY, 18490=XY 20490=XY, 18990=XY, 20990=XY}我们找到的所有价格和产品: {20720=XY, 11490=XY, 13490=XY, 15490=XY, 19490=XY, 21990=XY, 16490=XY, 18490=XY 20490=XY, 18990=XY, 20990=坐标}

As I think my code just does not write out or collect when the price is the same with other brands(or with the same brands too).因为我认为当价格与其他品牌(或相同品牌)相同时,我的代码不会写出或收集。 For example: At the website there are more products for 13490.. and lots of for 18490.例如:网站上有更多 13490.. 和 18490 的产品。

How can I modify my code in order to get all the pairs even if they have same prices?我怎样才能修改我的代码以获得所有对,即使它们具有相同的价格?

@Peter Santa, you should have product Name as your key instead of Price. @Peter Santa,您应该将产品名称作为密钥而不是价格。 So you can reverse the location.所以你可以反转位置。 But this will not print the same product with the same or different price.但这不会以相同或不同的价格打印相同的产品。 Which I feel should be ok assuming, the same product will not have different prices on the same website.我觉得应该可以假设,相同的产品在同一网站上不会有不同的价格。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM