无法将产品添加到价格差异超过50的购物车中

时间:2017-07-17 08:35:46

标签: xpath selenium-webdriver

我想将prodcut添加到原始折扣价格差异超过50的购物车中。我尝试使用以下代码。正在计算价格差异但不点击该项目。

有人可以帮助找出解决方案 另外,在这里附上图片。enter image description here

getting error when apply logic of calculating difference>>>
======================================
package Tekbakertest;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import com.relevantcodes.extentreports.LogStatus;

public class Ted_sale {
static WebDriver driver;

    public static void main(String[] args) throws InterruptedException {
        // TODO Auto-generated method stub
        System.setProperty("webdriver.chrome.driver","C:\\Chrome\\chromedriver_win32\\chromedriver.exe");
        //System.setProperty("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.Jdk14Logger");
     Thread.sleep(2000);
    ChromeOptions options = new ChromeOptions();
    Map<String, Object> prefs = new HashMap<String, Object>();
    prefs.put("credentials_enable_service", false);
    prefs.put("password_manager_enabled", false); 
      options.setExperimentalOption("prefs", prefs);
    options.addArguments("start-maximized");
    options.addArguments("disable-infobars");
    driver = new ChromeDriver(options);
    driver.get("http://www.tedbaker.com/");
    WebDriverWait wait = new WebDriverWait(driver, 40);
    wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("html/body/div[3]/div[2]/div/div/ul/li[1]/a")));

            if(driver.findElement(By.xpath("html/body/div[3]/div[2]/div/div/ul/li[1]/a")).isDisplayed())
                {

                    driver.findElement(By.xpath("html/body/div[3]/div[2]/div/div/ul/li[1]/a")).click();

                }
    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']"));
      System.out.println(Product_list.size());
      for(int i=0;i<Product_list.size();i++)
      {
           List<String> Product_list_lst = new ArrayList<String>();
        Product_list_lst.add(Product_list.get(i).getText());
       System.out.println(Product_list.get(i).getText());
         }
            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)
        {

                    Product_list.get(i).click();
                    // driver.findElement(By.xpath("//input[contains(@class,'add_to_cart')]")).click();
        }
  }
        }       
}


error as>>

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 59, Size: 59
    at java.util.ArrayList.rangeCheck(Unknown Source)
    at java.util.ArrayList.get(Unknown Source)
    at Tekbakertest.Ted_sale.main(Ted_sale.java:92)
[enter image description here][1]


  [1]: https://i.stack.imgur.com/QqQSY.jpg

1 个答案:

答案 0 :(得分:0)

product with out any discounts

如果您收到错误,请检查图像。实际上有一种产品没有任何折扣价。因此,折扣价格和原始价格数组之间存在差异。您需要在向数组添加价格之前添加某种验证(例如,如果折扣价格元素不存在则将零添加到该特定折扣价格)。

现在回答您真正的问题,之所以没有点击产品名称是因为您点击了标题。试试这段代码,

Product_list.get(i).findElement(By.tagName(a)).click();

让我知道这是否有效。