阅读硒中的WebElement列表 [英] Reading List of WebElements in selenium
问题描述
我是硒的新手,并且我关注以下站点: http://live.guru99.com/index.php/mobile.html
i'm novice with selenium and i've following site: http://live.guru99.com/index.php/mobile.html
我要使用pagefactory将所有商品的实际价格都收集到一个列表中.
Using pagefactory I I want to get all actual prices of items in one list.
@FindBy (css=".price") List<WebElement> prices;
public ArrayList<String> getPrices(){
ArrayList<String> pricesList = new ArrayList<>();
for (WebElement we:prices){
pricesList.add(we.getText());
}
return pricesList;
}
上面的代码可以给我4个价格,2个正常+ 1个旧价格和1个称为特价"的价格 如何更改代码以仅接收2个正常价格和1个特价价格?我不需要旧的价格值.
Code above can give me 4 prices, 2 normal + 1 old and 1 called 'special offer' How can i change code to receive only 2 normal and 1 special offer price? I don't need old values of prices.
上面的代码是否适合使用pagefactory从站点读取相似的值?
By the way code above is good for reading similar values from site using pagefactory?
当我获得这3个价格时,我想将所有值与商品网站上的价格进行比较,我想我知道该怎么做,但是问题出在我的WebElements列表中是4个而不是3个值
When i get these 3 prices i want to compare all values with prices on item site, i think I know how to do it, but problem is in 4 instead of 3 values in my List of WebElements
推荐答案
尝试使用此CSS选择器
Try to use this css selector
@FindBy(css=".price-box .regular-price,.special-price")
更新了更严格的解决方案
Updated with more rigid solutions
@FindBy (css=".price-box") List<WebElement> prices;
public ArrayList<String> getPrices(){
ArrayList<String> pricesList = new ArrayList<>();
for (WebElement we : prices){
// Check if there is an old-price
if (we.findElements(By.css('.old-price')).size() != 0) {
// Assuming you will take special price if there's an old-price
priceList.add(we.findElement(By.css('.special-price')).getText());
} else {
pricesList.add(we.getText());
}
}
return pricesList;
}
这篇关于阅读硒中的WebElement列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!