【问题标题】:unable to get list of products by using xpath无法使用 xpath 获取产品列表
【发布时间】:2019-12-09 10:34:59
【问题描述】:

任务-打印来自亚马逊的所有产品描述和价格 我使用列表并使用 for 循环对其进行迭代

发生 - 它只打印一个产品信息和价格并打印它们直到 for 循环匹配条件

代码:

List<WebElement> products = driver.findElements(By.xpath("//div[@class=\"s-result-list s-search-results sg-row\"]//descendant::span[@class=\"a-size-base-plus a-color-base a-text-normal\"]"));
        int count = products.size();
        System.out.println(count);


    //using XLS reader class to make runtime excel sheet and add column
    Xls_Reader reader = new Xls_Reader(prop.getProperty("Excel"));
    if (!reader.isSheetExist("ProductData")) {
        reader.addSheet("ProductData");
        reader.addColumn("ProductData", "Name of Product");
        reader.addColumn("ProductData", "Price of Product");
    }

    // for loop to set cell data of product description and product price
    for (int i = 0; i <= count; i++) {
        String prod_desc = driver.findElement(By.xpath("//span[@class=\"a-size-base-plus a-color-base a-text-normal\"]")).getText();
        System.out.println("Done111");
        reader.setCellData("ProductData", "Name of Product", i, prod_desc);

        String prod_price= driver.findElement(By.xpath("//div[@class=\"a-section a-spacing-none a-text-center\"]//descendant::span[@class=\"a-price-whole\"]"
        )).getText(); System.out.println(prod_price);
        reader.setCellData("ProductData", "Price of Product", i, prod_price);            
    }
    }
    catch(Exception e) {
        e.printStackTrace();
    }

结果:

Amazon.in: wrist watches
26
Done111
3,746
Done111
3,746
Done111
3,746
Done111
3,746
Done111
3,746
Done111
3,746
Done111
3,746
Done111
3,746
Done111
3,746
Done111
3,746
Done111
3,746
Done111
3,746
Done111
3,746
Done111
3,746
Done111
3,746
Done111
3,746
Done111
3,746
Done111
3,746
Done111
3,746
Done111
3,746
Done111
3,746
Done111
3,746
Done111
3,746
Done111
3,746
Done111
3,746
Done111
3,746
Done111
3,746
PASSED: clickOnSearchBtnTest

【问题讨论】:

  • 你有什么问题?
  • 它只打印一个产品信息。我必须使用 for 循环打印到网页上的所有产品信息
  • 您在 for 循环中有 desc 和 price 抓取,但它们在循环中是否发生变化?

标签: java selenium selenium-webdriver web-applications


【解决方案1】:

你能添加 html.. 它将有助于解决你的问题

同时尝试以下操作:

for(WebElement product : products) {
  product.getText();
}

【讨论】:

  • 在第一个网页上仅查找指定商品的产品描述和价格并打印出来
【解决方案2】:

使用以下 xpath,您将获得第一页中所有描述的列表

List<WebElement> descriptions = driver.findElements(By.xpath(".//span[@class='a-size-base-plus a-color-base a-text-normal']"));

使用下面的 xpath 你会得到所有价格的列表

List<WebElement> prices = driver.findElements(By.xpath(".//span[@class='a-price-whole']"));

由于描述列表和价格列表的计数相同,因此我们将迭代循环并从两个列表中的每个元素中获取文本。

for(int i = 0; i < descriptions.size(); i++) {
        String desc = descriptions.get(i).getText();
        String price = prices.get(i).getText();
        System.out.println(desc +":"+ price);
}

【讨论】:

  • 虽然这可能会回答问题,但仅代码回答并不能真正解释您选择该代码的原因或您的代码如何解决问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-30
  • 1970-01-01
  • 2023-03-05
  • 2012-03-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多