我无法迭代列表项,直到我在selenium webdriver中找到使用for循环的预期项。怎么做?

时间:2017-08-19 06:54:23

标签: java selenium-webdriver

我正在获取物品清单&搜索该项目。如果它在那里打印项目的价格并点击图像。

我的java代码片段:

List<WebElement> items = driver.findElements(By.xpath("//*[@class='category-products']/descendant::li[@class='item last']"));
int noOfItems = items.size();
System.out.println(noOfItems);
String price = null;
//iterate item list till the product name Sony Xperia not found
for (int i = 0; i < noOfItems; i++) {
    //check product name sony xperia
    WebElement item = driver.findElement(By.xpath("//*[@class='category-products']/descendant::h2[@class='product-name']/a[" + i + "]"));
    String name = item.getText();
    //System.out.println("Product Name: "+name);
    //if found then click on its image link         
    if (name.equals("Sony Xperia")) {
        WebElement prodPrice = driver.findElement(By.xpath("//*[@class='category-products']/descendant::div[@class='price-box'][" + i + "]/descendant::span[@class='price']"));
        price = prodPrice.getText();
        System.out.println(price);
        WebElement itemImg = driver.findElement(By.xpath("//*[@class='category-products']/descendant::a[@class='product-image'][" + i + "]"));
        itemImg.click();
    }
}

0 个答案:

没有答案