【发布时间】: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