2017-07-17 42 views
0

我想将产品添加到原价和折扣价格差异超过50的购物车中。我尝试了以下代码。它正在计算价格差异,但未点击在物品上。无法将产品添加到价格差异超过50的购物车中

有人可以帮忙找出解决方案 此外,attachimh图像在这里。 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 
+0

条件是'我 Guy

回答

0

product with out any discounts

对于错误你得到,请检查图像。实际上有一种产品没有折扣价。因此,折扣价格与原价格阵列的尺寸存在差异。您需要在向阵列添加价格之前添加某种验证(例如,如果折扣价格元素不存在,则将该特定折扣价格加零)。

现在来到您真正的问题,它不点击产品名称是因为您点击标题。试试这个代码,

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

让我知道这是否工作。

+0

是的,它只将一种产品加入购物车,无法识别剩余产品。如果有4个产品的价格差异大于50,那么它就是第一个。 – demouserabcd

相关问题