Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java中的Selenium-返回的元素数大于页面上的元素数_Java_Selenium_Web Scraping - Fatal编程技术网

Java中的Selenium-返回的元素数大于页面上的元素数

Java中的Selenium-返回的元素数大于页面上的元素数,java,selenium,web-scraping,Java,Selenium,Web Scraping,下面的HTML部分来自AboutYou.de()的搜索结果,我正试图从这个网站上搜索。我已确定每个项目的a元素以a href=“/p/”开头。页面源中有98个。 例如,这是一项: <a href="/p/hugo/buegelleichtes-hemd-elvor-3636873"> 为了获得这98个项目,我在Java应用程序中使用了以下代码行: List<WebElement> pageContent = new ArrayList<WebElement&g

下面的HTML部分来自AboutYou.de()的搜索结果,我正试图从这个网站上搜索。我已确定每个项目的a元素以a href=“/p/”开头。页面源中有98个。 例如,这是一项:

<a href="/p/hugo/buegelleichtes-hemd-elvor-3636873">

为了获得这98个项目,我在Java应用程序中使用了以下代码行:

List<WebElement> pageContent = new ArrayList<WebElement>();

pageContent.addAll(driver.findElements(By.cssSelector("a[href*='/p/']")));
List pageContent=new ArrayList();
pageContent.addAll(driver.findElements(By.cssSelector(“a[href*='/p/']));

结果是196个,或者是实际存在的两倍。事实上,如果我使用查询匹配,例如,10个元素,Selenium将返回20个,依此类推。列表的大小将始终是匹配元素的两倍。如何仅获得98个,并确保这些是我实际打算使用的元素?

这是因为e非常artikel有两个链接,一个在图片上,一个在artikel的标题上。 您可以使用更具体的路径,如:

List<WebElement> pageContent = new ArrayList<WebElement>();

pageContent = driver.findElements(By.xpath("//a[contains(@href, '/p/')][@class='product-name-link']");
List pageContent=new ArrayList();
pageContent=driver.findElements(By.xpath(//a[contains(@href,“/p/”)][@class='product-name-link']);
html示例

<article id="product-3636873" class="js-productlist-product productlist-product isLayout3  " data-product-id="3636873" data-product-variant="33638894" data-product-brand="3083">
    <div class="badges">
        <ul class="badges-list category-badges-list">
                                </ul>
        <ul class="badges-list js-badges-list">
                        <li><span class="badge sale js-badge-sale hidden" data-original="hidden"></span></li>
                            <li><span class="badge new js-badge-new">Neu</span></li>
                    </ul>
    </div>
    <div class="js-wishlist-button-region wishlist-button-region"></div>
    <div class="product-image loaded  ">



69,90

在您的浏览器控制台中,尝试运行以下命令:
document.querySelectorAll(“a[href*='/p/'])。length
您得到的数字是196。但是如果通过CTRL+F启动搜索,结果是98,如果我只计算元素的数量,结果就是98。非常感谢。
      <a href="/p/hugo/buegelleichtes-hemd-elvor-3636873">
                        <picture>
                <source srcset="//cdn.aboutyou.de/file/78305ba727523be0c92ec529b9776f17?bg=ffffff&amp;height=400&amp;progressive=1&amp;quality=90&amp;trim=1&amp;width=400 1x, //cdn.aboutyou.de/file/78305ba727523be0c92ec529b9776f17?bg=ffffff&amp;height=600&amp;progressive=1&amp;quality=90&amp;trim=1&amp;width=600 2x">
                <img src="//cdn.aboutyou.de/file/78305ba727523be0c92ec529b9776f17?bg=ffffff&amp;height=400&amp;progressive=1&amp;quality=90&amp;trim=1&amp;width=400" data-src="//cdn.aboutyou.de/file/78305ba727523be0c92ec529b9776f17?bg=ffffff&amp;height=400&amp;progressive=1&amp;quality=90&amp;trim=1&amp;width=400" class="product-original js-productlist-previewimage loaded" width="245" height="211" alt="HUGO Bügelleichtes Hemd 'Elvor'">
            </picture>
                    </a>
    </div>

    <!-- Adding new thumbs for touch devices -->
    <div class="js-product-touch-variants product-touch-variants">
    </div>

    <!-- TODO: need to get real data here and show it only if campaign is active otherwise hidden -->
        <div class="js-product-name product-name">
        <div itemprop="name">
<a class="product-name-link" itemprop="url" href="/p/hugo/buegelleichtes-hemd-elvor-3636873">Bügelleichtes Hemd 'Elvor'</a>
</div>
    </div>
        <div class="product-brand">
        <div itemprop="brand"><a class="js-product-item-brand" href="?gender=null">HUGO</a></div>
    </div>
    <div class="js-product-price product-price" itemprop="offers" itemscope="" itemtype="http://schema.org/Offer">

    <meta itemprop="priceCurrency" content="EUR"><h5 class="price actual-price actual-price" itemprop="price" content="69.90">
    <span class="price-data currency isEUR" data-original="69,90€" data-price="69,90€">69,90</span>
</h5>
    </div>
</article>