首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法将差价超过50的产品添加到购物车

问无法将差价超过50的产品添加到购物车
EN

Stack Overflow用户
提问于 2017-07-17 16:35:46
回答 1查看 51关注 0票数 0

我想要添加prodcut到购物车,其原始和折扣价格差异超过50。我已经尝试用下面的code.it计算差价,但没有点击项目。

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

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

发布于 2017-07-17 17:28:55

​

​

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

现在转到你真正的问题,它没有点击产品名称的原因是因为你点击了标题。试试这段代码,

代码语言:javascript
复制
Product_list.get(i).findElement(By.tagName(a)).click();

如果有效,请让我知道。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45139468

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档