2017-07-17 6 views
0

オリジナルと割引価格の差額が50を超えるカートに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 
+0

条件は 'i Guy

答えて

0

product with out any discounts

あなたは画像をご確認ください、取得しています。実際には割引価格のない商品があります。したがって、割引価格と原価の配列のサイズには違いがあります。配列に価格を追加する前に何らかのバリデーションを追加する必要があります(たとえば、割引価格要素が存在しない場合、その特定の割引価格にゼロを追加するなど)。

実際に質問に来ると、商品の名前をクリックしていない理由は、見出しをクリックしているためです。このコードを試してみてください。

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

これが動作するかどうかを教えてください。

+0

はい、商品をカートに1つだけ追加しているため、商品を識別できません。価格差が> 50の商品が4つある場合は、1番目の商品を購入しています。 – demouserabcd

関連する問題