当比较两个值时,它显示字符串索引超出范围:-1

我想要从 flipkart 和亚马逊网站比较的商品的最低价格。我正在这两个网站上搜索特定的手机


    public static void main(String arg[]) throws InterruptedException {

        ArrayList<String[]> data1 = new ArrayList<String[]>();

        String[] entriesArr = null;

        String[] entriesArr1 = null;  System.setProperty("webdriver.chrome.driver","C:\\Seleniumjava\\driver\\chromedriver.exe");

           WebDriver driver = new ChromeDriver();                   driver.manage().timeouts().implicitlyWait(20,TimeUnit.SECONDS);

        //open flipkart

          driver.get("https:/www.flipkart.com");

//xpath for close the popup

           driver.findElement(By.xpath("//button[text()='✕']")).click();

                    driver.findElement(By.xpath("//div[@class='O8ZS_U']/input")).sendKeys("Redmi 7 ( 32 MB , 2 GB ) Black");

                    driver.findElement(By.xpath("//div[@class='O8ZS_U']/input")).sendKeys(Keys.ENTER);

                    List<WebElement> phonenames = driver.findElements(By.xpath("//div[@class='_3wU53n']"));

                    List<WebElement> phoneprice = driver.findElements(By.xpath("//div[@class='_1vC4OE _2rQ-NK']"));

                    for( int i=0;i<phonenames.size();i++)

                    {

                        if(phonenames.get(i).getText().contains("Redmi 7 (Eclipse Black, 32 GB)")) 

                        {

                            entriesArr = new String[]{phonenames.get(i).getText().toString(), phoneprice.get(i).getText().toString() };

                            data1.add(entriesArr);

                        }

                    }

当我运行我的代码时,它显示 String index out of range: -1 in the line int value = Integer.parseInt(data.get(i)[1].substring(1).replace(",", "")) ;


慕的地6264312
浏览 107回答 1
1回答

桃花长相依

我添加了一些sleep for wait a load page enter click后,你错误的选择了一个定位器By.xpath("//span[@class='a-offscreen']"),定位器没有给出任何文本="",所以当你使用.substring(1)它时会导致错误String index out of range: -1,请试试下面的代码。public static void main(String arg[]) throws InterruptedException {&nbsp; &nbsp; ArrayList<String[]> data1 = new ArrayList<String[]>();&nbsp; &nbsp; String[] entriesArr = null;&nbsp; &nbsp; String[] entriesArr1 = null;&nbsp;&nbsp;&nbsp; &nbsp; System.setProperty("webdriver.chrome.driver","C:\\Seleniumjava\\driver\\chromedriver.exe");&nbsp; &nbsp; WebDriver driver = new ChromeDriver();&nbsp; &nbsp; driver.manage().timeouts().implicitlyWait(20,TimeUnit.SECONDS);&nbsp; &nbsp; //open flipkart&nbsp; &nbsp; driver.get("https://www.flipkart.com");&nbsp; &nbsp; //xpath for close the popup&nbsp; &nbsp; driver.findElement(By.xpath("//button[text()='✕']")).click();&nbsp; &nbsp; driver.findElement(By.xpath("//div[@class='O8ZS_U']/input")).sendKeys("Redmi 7 ( 32 MB , 2 GB ) Black");&nbsp; &nbsp; driver.findElement(By.xpath("//div[@class='O8ZS_U']/input")).sendKeys(Keys.ENTER);&nbsp; &nbsp; //I'm edit here&nbsp; &nbsp; Thread.sleep(5000);&nbsp; &nbsp; List<WebElement> phonenames = driver.findElements(By.xpath("//div[@class='_3wU53n']"));&nbsp; &nbsp; List<WebElement> phoneprice = driver.findElements(By.xpath("//div[@class='_1vC4OE _2rQ-NK']"));&nbsp; &nbsp; for( int i=0;i<phonenames.size();i++){&nbsp; &nbsp; &nbsp; &nbsp; if(phonenames.get(i).getText().contains("Redmi 7 (Eclipse Black, 32 GB)")) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; entriesArr = new String[]{phonenames.get(i).getText().toString(), phoneprice.get(i).getText().toString() };&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; data1.add(entriesArr);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; driver.close();&nbsp; &nbsp; System.setProperty("webdriver.chrome.driver","C:\\Seleniumjava\\driver\\chromedriver.exe");&nbsp; &nbsp; WebDriver driver1 = new ChromeDriver();&nbsp; &nbsp; driver1.manage().timeouts().implicitlyWait(20,TimeUnit.SECONDS);&nbsp; &nbsp; //open amazon site&nbsp; &nbsp; driver1.get("https:/www.amazon.in");&nbsp; &nbsp; driver1.findElement(By.xpath("//input[@id='twotabsearchtextbox']")).sendKeys("Redmi 7 ( 32 MB , 2 GB ) Black");&nbsp; &nbsp; driver1.findElement(By.xpath("//input[@id='twotabsearchtextbox']")).sendKeys(Keys.ENTER);&nbsp; &nbsp; Thread.sleep(3000);&nbsp; &nbsp; List< WebElement> phonenames2 = driver1.findElements(By.xpath("//a//span[@class='a-size-medium a-color-base a-text-normal']"));&nbsp; &nbsp; //I'm edit here&nbsp; &nbsp; List< WebElement> phoneprice1 = driver1.findElements(By.xpath("//*[@class='a-price' and @data-a-size='l']"));&nbsp; &nbsp; for( int j=0;j<phonenames2.size();j++) {&nbsp; &nbsp; &nbsp; &nbsp; if(phonenames2.get(j).getText().contains("Redmi 7 (Eclipse Black, 2GB RAM, 32GB Storage)")) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; entriesArr1 = new String[]{phonenames2.get(j).getText().toString(), phoneprice1.get(j).getText().toString() };&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; data1.add(entriesArr1);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; driver1.close();&nbsp; &nbsp; String[] d = getMinPhonePrice(data1);&nbsp; &nbsp; System.out.println(d[0] + ", " + d[1]);}//method for minimum phone pricepublic static String[] getMinPhonePrice(ArrayList<String[]> data){&nbsp; &nbsp; int value_min = Integer.parseInt(data.get(0)[1].substring(1).replace(",", ""));&nbsp; &nbsp; String key_min="";&nbsp; &nbsp; for(int i=1;i<data.size();i++) {&nbsp; &nbsp; &nbsp; &nbsp; int value = Integer.parseInt(data.get(i)[1].substring(1).replace(",", ""));&nbsp; &nbsp; &nbsp; &nbsp; if(value < value_min) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; value_min=value;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; key_min=data.get(i)[0];&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; String[] d = {key_min,String.valueOf(value_min)};&nbsp; &nbsp; return d;}&nbsp;
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java