识别重复的搜索结果,这些结果显示在多个页面中

时间:2018-01-16 22:26:30

标签: java selenium-webdriver

我是这个论坛以及Selenium的新手。请帮我解决以下问题:

  1. 我想比较购物网站的搜索结果,这些搜索结果显示在多个页面中。我想确保我的搜索字符串结果只是唯一值。

  2. 当我尝试打印第一页中显示的搜索结果时,会产生264个项目;虽然第一页上只列出了60个项目。

  3. 页面中显示的某些产品不会打印,而是会打印一些在应用程序中不可见的项目。我真的怀疑它是否也在考虑禁用div标签。

  4. 在下面的html中,div标签class="block-grid-xs-3 hide-xs hide-sm hide-md hide-xl hide-tv prolist-row pb-xs-1-5"处于"禁用"州,同样,此HTML代码中有许多禁用的标记:

    
    
    <div class="col-xs-12 pl-xs-1 pl-md-3">
    <div class="clearfix pb-xs-1-5">
    <h2 class="screen-reader-only">Search results</h2>
    <div class="block-grid-xs-2 hide-lg hide-xl hide-tv prolist-row pb-xs-1-5" data-behat-search-results-ads-xs="">
    <div class="block-grid-xs-3 hide-xs hide-sm hide-md hide-xl hide-tv prolist-row pb-xs-1-5" data-behat-search-results-ads-lg="">
    <div class="js-merch-stash-check-listing block-grid-item v2-listing-card position-relative " data-palette-listing-id="548623069" data-shop-id="" data-listing-id="548623069" data-behat-listing-card="">
    <a class="prolist display-inline-block listing-link logged" data-listing-id="548623069" data-palette-listing-image="" href="https://www.etsy.com/listing/548623069/elegant-ivory-flower-girl-dressivory?ga_order=most_relevant&ga_search_type=all&ga_view_type=gallery&ga_search_query=flower girl dress&ref=sc_gallery-1-1&plkey=5dd934ef34daf3e1dfec620406d1cf14b94e9cf8:548623069" data-display-loc="w.0" data-page-num="1" data-position-num="1" data-logging-key="5dd934ef34daf3e1dfec620406d1cf14b94e9cf8:548623069" data-behat-search-ad-link="" title="elegant ivory flower girl dress,ivory flower girl dress,white flower girl dress,rustic flower girl dress,tutu flower girl dress">
    <div class="v2-listing-card__img overflow-hidden mb-xs-1 position-relative">
    <div class="v2-listing-card__info">
    <p class="text-gray text-truncate mb-xs-0 text-body"> elegant ivory flower girl dress,ivory flower girl dress,white flower girl dress,rustic flower girl dress,tutu flower girl dress </p>
    <div class="v2-listing-card__shop">
    <p class="n-listing-card__price text-gray strong mt-xs-0">
    </div>
    </a>
    <div class="v2-listing-card__actions z-index-1 position-absolute">
    </div>
    <div class="js-merch-stash-check-listing block-grid-item v2-listing-card position-relative " data-palette-listing-id="206144142" data-shop-id="" data-listing-id="206144142" data-behat-listing-card="">
    <div class="js-merch-stash-check-listing block-grid-item v2-listing-card position-relative " data-palette-listing-id="523280690" data-shop-id="" data-listing-id="523280690" data-behat-listing-card="">
    </div>
    <div class="block-grid-xs-4 hide-xs hide-sm hide-md hide-lg prolist-row pb-xs-1-5" data-behat-search-results-ads-xl="">
    <div class="js-merch-stash-check-listing block-grid-item v2-listing-card position-relative " data-palette-listing-id="548623069" data-shop-id="" data-listing-id="548623069" data-behat-listing-card="">
    <a class="prolist display-inline-block listing-link logged" data-listing-id="548623069" data-palette-listing-image="" href="https://www.etsy.com/listing/548623069/elegant-ivory-flower-girl-dressivory?ga_order=most_relevant&ga_search_type=all&ga_view_type=gallery&ga_search_query=flower girl dress&ref=sc_gallery-1-1&plkey=5dd934ef34daf3e1dfec620406d1cf14b94e9cf8:548623069" data-display-loc="w.0" data-page-num="1" data-position-num="1" data-logging-key="5dd934ef34daf3e1dfec620406d1cf14b94e9cf8:548623069" data-behat-search-ad-link="" title="elegant ivory flower girl dress,ivory flower girl dress,white flower girl dress,rustic flower girl dress,tutu flower girl dress">
    <div class="v2-listing-card__img overflow-hidden mb-xs-1 position-relative">
    <div class="v2-listing-card__info">
    <p class="text-gray text-truncate mb-xs-0 text-body" style="background-color: rgba(0, 0, 0, 0);"> elegant ivory flower girl dress,ivory flower girl dress,white flower girl dress,rustic flower girl dress,tutu flower girl dress </p>
    <div class="v2-listing-card__shop">
    <p class="n-listing-card__price text-gray strong mt-xs-0">
    </div>
    </a>
    &#13;
    &#13;
    &#13;

    这是我的代码,用于查找和打印第一页中的项目:

    List<WebElement> searchres=d.findElements(By.cssSelector("div.v2-listing-card__info>p"));
    int o=0;
    String b[]=new String[searchres.size()];
    for(WebElement h: searchres )
        {
            b[o]=h.getText().trim();
            System.out.println(h.getText());
            o++;
        }
    
    HashMap <Integer, String> map= new HashMap<Integer, String>();
    
        for(int i=0;i<b.length;i++)
        {
            if(map.containsKey(b[i]))
            {
                System.out.println("duplicate");
            }
    
            map.put(i,b[i]);            
        }
    

1 个答案:

答案 0 :(得分:0)

看起来你试图用div“v2-listing-card__info”找到div中的所有“p”元素,并且因为div中有几个p元素,你得到的结果比需要的多。