如何在selenium webdriver中找到产品的原始和折扣价格差异

时间:2017-07-13 05:10:10

标签: java selenium-webdriver

我试图找出电子商务网站上任何产品的原始价格和折扣价格之间的差异。你能建议我怎么计算吗?  价格的Html代码如下:

 <html>
<ul class="pricing ng-scope" ng-if="activeColour.colourCode == '133290-LT-GREY'">
    <li class="price previous">
    <del>£99</del>
    </li>
    <li class="price unit">£69</li>
    </ul>
<html>

============================================== <登记/> 代码找出价格差异大于50的产品并将该产品添加到购物车

Actions act=new Actions(driver);
WebElement Category=driver.findElement(By.xpath(".//*[@id='categories_nav']/li[1]/a"));
       WebElement Sales_clothng=driver.findElement(By.xpath(".//*[@id='categories_nav']/li[1]/nav/div[1]/div[3]/ul[1]/li[3]/a"));
         act.moveToElement(Category).moveToElement(Sales_clothng).click().build().perform();
       Thread.sleep(2000);
          List<WebElement> Product_list=driver.findElements(By.xpath(".//*[@class='name']"));
                for (WebElement items_product : Product_list)
        {
            System.out.println(items_product.getText());

            }
       System.out.println("Originanal Price of items");
       List<WebElement> Original_price=driver.findElements(By.xpath(".//*[@class='price previous']"));
       System.out.println(Original_price.size());
          for(int i=0;i<Original_price.size();i++)
       {
               List<String> Original_price_lst = new ArrayList<String>();
           Original_price_lst.add(Original_price.get(i).getText());
           System.out.println(Original_price.get(i).getText());
             }
          System.out.println("Discounted Price of items");
       List<WebElement> Discounted_price=driver.findElements(By.xpath(".//*[@class='price unit']"));
       System.out.println(Discounted_price.size());
       for(int i=0;i<Original_price.size();i++)
       {
               List<String> Discounted_price_lst = new ArrayList<String>();
           Discounted_price_lst.add(Discounted_price.get(i).getText());
           System.out.println(Discounted_price.get(i).getText());
       }
      for(int i=0;i<Discounted_price.size();i++)
      {
          List<String> Original_price_lst_1 = new ArrayList<String>();
          List<String> Discounted_price_lst_1 = new ArrayList<String>();
          String original_price_removecurrencysymbol = Original_price.get(i).getText().substring(1,Original_price.get(i).getText().length());
           String discount_price_removecurrencysymbol = Discounted_price.get(i).getText().substring(1,Discounted_price.get(i).getText().length());
          System.out.println(original_price_removecurrencysymbol);
          System.out.println(discount_price_removecurrencysymbol);
          int difference = Integer.parseInt(original_price_removecurrencysymbol) -Integer.parseInt(discount_price_removecurrencysymbol);
            System.out.println("Difference is:"+difference);
                    if(difference>=50)
            {
                        driver.findElement(By.xpath(".//*[@class='name']")).click();
                         driver.findElement(By.xpath("//input[contains(@class,'add_to_cart')]")).click();
            }
      }
            }       
    }

3 个答案:

答案 0 :(得分:0)

在单独的变量中获取价格,即原始和折扣,并将它们转换为整数。您还需要在转换为整数之前删除货币符号。现在您可以减去它们以获得差异。

答案 1 :(得分:0)

一个班轮。

 int diff =Integer.parseInt(driver.findElement(By.xpath(".//*[@id='content']/div/div[4]/div[2]/div[2]/article/div[2]/header/div/ul/li[1]/del")).getText().substr(1))-Integer.parseInt(driver.findElement(By.className("price unit").getText()substr(1)));

答案 2 :(得分:0)

工作代码:

        WebDriver driver = new FirefoxDriver();
        driver.get("http://www.tedbaker.com/uk/Womens/Sale/Accessories/Bags/c/sale_bags");

        String original_price = driver.findElement(By.xpath(".//*[@id='content']/div/div[4]/div[2]/div[2]/article/div[2]/header/div/ul/li[1]/del")).getText();
        String discount_price = driver.findElement(By.xpath(".//*[@id='content']/div/div[4]/div[2]/div[2]/article/div[2]/header/div/ul/li[2]")).getText();

        System.out.println(original_price);
        System.out.println(discount_price);

        String original_price_removecurrencysymbol = original_price.substring(1,original_price.length());
        String discount_price_removecurrencysymbol = discount_price.substring(1,discount_price.length());

        System.out.println(original_price_removecurrencysymbol);
        System.out.println(discount_price_removecurrencysymbol);

        int difference = Integer.parseInt(original_price_removecurrencysymbol) -Integer.parseInt(discount_price_removecurrencysymbol);

        System.out.println("Difference is:"+difference);