使用 selenium webdriver 测试排序的项目(从高到低的价格)是否正确显示
To test sorted items(High to Low price) are displayed properly using selenium webdriver
这是我要测试的网站:
http://tutorialsninja.com/demo/index.php?route=product/category&path=18&sort=p.price&order=DESC
价格从高到低排序。我必须检查排序是否正确。
第一个问题是我找不到合适的选择器(我只需要主要价格)
我这里有一些代码:
// scrape price elements
List<WebElement> price = driver.findElements(By.xpath(*--DONT HAVE THE SELECTOR--*));
// extract the prices from the price elements and store in a List
List<String> prices = new ArrayList<String>();
for (WebElement e : price)
{
prices.add(e.getText());
}
// make a copy of the list
List<String> sortedPrices = new ArrayList<String>(prices);
// sort the list
Collections.sort(sortedPrices);
// true if the prices are sorted
System.out.println(sortedPrices.equals(prices));
assertEquals(prices, sortedPrices);
试试这个 css,div.caption p.price
:
List<WebElement> price = driver.findElements(By.cssSelector("div.caption p.price"));
并像这样打印值:
List<WebElement> price = driver.findElements(By.cssSelector("div.caption p.price"));
for(WebElement e : price){
System.out.println(e.getText().split("\$")[1].split("\.")[0].replace(",", ""));
prices.add(e.getText().split("\$")[1].split("\.")[0].replace(",", ""));
}
试试下面的代码。
xPath
所有商品的价格,
//div[@id='content']//p[@class='price']//span
List<WebElement> price = driver.findElements(By.xpath("//div[@id='content']//p[@class='price']//span"));
/* extract the prices from the price elements and store in a List */
List<String> prices = new ArrayList<String>();
for (WebElement e : price)
{
prices.add(e.getText());
}
/* make a copy of the list */
List<String> sortedPrices = new ArrayList<String>(prices);
/* sort the list */
Collections.sort(sortedPrices);
/* true if the prices are sorted */
System.out.println(sortedPrices.equals(prices));
assertEquals(prices, sortedPrices);
我会选择以下代码:(在 Python 中)
driver.implicitly_wait(10)
driver.get("http://tutorialsninja.com/demo/index.php?route=product/category&path=18&sort=p.price&order=DESC")
price = driver.find_elements_by_class_name("price")
pricelist = []
for p in price:
pri = p.text.split('\n')[0].split('.')[0].replace('$','').replace(',', '')
pricelist.append(int(pri))
print(pricelist)
输出:
[2000, 1202, 1202, 602, 122]
这是我要测试的网站: http://tutorialsninja.com/demo/index.php?route=product/category&path=18&sort=p.price&order=DESC
价格从高到低排序。我必须检查排序是否正确。
第一个问题是我找不到合适的选择器(我只需要主要价格)
我这里有一些代码:
// scrape price elements
List<WebElement> price = driver.findElements(By.xpath(*--DONT HAVE THE SELECTOR--*));
// extract the prices from the price elements and store in a List
List<String> prices = new ArrayList<String>();
for (WebElement e : price)
{
prices.add(e.getText());
}
// make a copy of the list
List<String> sortedPrices = new ArrayList<String>(prices);
// sort the list
Collections.sort(sortedPrices);
// true if the prices are sorted
System.out.println(sortedPrices.equals(prices));
assertEquals(prices, sortedPrices);
试试这个 css,div.caption p.price
:
List<WebElement> price = driver.findElements(By.cssSelector("div.caption p.price"));
并像这样打印值:
List<WebElement> price = driver.findElements(By.cssSelector("div.caption p.price"));
for(WebElement e : price){
System.out.println(e.getText().split("\$")[1].split("\.")[0].replace(",", ""));
prices.add(e.getText().split("\$")[1].split("\.")[0].replace(",", ""));
}
试试下面的代码。
xPath
所有商品的价格,
//div[@id='content']//p[@class='price']//span
List<WebElement> price = driver.findElements(By.xpath("//div[@id='content']//p[@class='price']//span"));
/* extract the prices from the price elements and store in a List */
List<String> prices = new ArrayList<String>();
for (WebElement e : price)
{
prices.add(e.getText());
}
/* make a copy of the list */
List<String> sortedPrices = new ArrayList<String>(prices);
/* sort the list */
Collections.sort(sortedPrices);
/* true if the prices are sorted */
System.out.println(sortedPrices.equals(prices));
assertEquals(prices, sortedPrices);
我会选择以下代码:(在 Python 中)
driver.implicitly_wait(10)
driver.get("http://tutorialsninja.com/demo/index.php?route=product/category&path=18&sort=p.price&order=DESC")
price = driver.find_elements_by_class_name("price")
pricelist = []
for p in price:
pri = p.text.split('\n')[0].split('.')[0].replace('$','').replace(',', '')
pricelist.append(int(pri))
print(pricelist)
输出:
[2000, 1202, 1202, 602, 122]