首页 > 解决方案 > 尝试在 pepperfy.com 上获取学习灯的价格时出现错误 ArrayIndexOutOfBoundsException

问题描述

我尝试执行相同的逻辑来从pepperfry.com 获取学习灯的报价,但我得到了ArrayIndexBoudn 异常。请找到以下代码:

代码

package com.Ecommerce;



import java.util.List;



import org.openqa.selenium.By;

import org.openqa.selenium.Keys;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.chrome.ChromeDriver;



public class PageLaunch {



public static void main(String[] args) throws Exception {

// TODO Auto-generated method stub

System.setProperty("webdriver.chrome.driver", "C://Users//484834//Downloads//chromedriver.exe");

WebDriver driver = new ChromeDriver();

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

driver.manage().window().maximize();

Thread.sleep(8000);

driver.switchTo().frame("notification-frame-22a3358b6");

driver.findElement(By.cssSelector("span.wewidgeticon.we_close.icon-large")).click();  // Close first pop Up

System.out.println("************Closed First Pop UP************");

driver.switchTo().defaultContent();

Thread.sleep(8000);

//driver.switchTo().alert().dismiss();

//driver.findElement(By.xpath("//*[@id='signinupPopupBox']/a")).click();

//System.out.println("************Closed Second Pop UP************");



driver.findElement(By.xpath("//input[@itemprop='query-input']")).sendKeys("study lamp");

driver.findElement(By.xpath("//input[@itemprop='query-input']")).sendKeys(Keys.ENTER);

System.out.println("************Study Lamps are Searched************");

Thread.sleep(8000);

driver.switchTo().frame("webklipper-publisher-widget-container-notification-frame");

driver.findElement(By.cssSelector("span.wewidgeticon.we_close.icon-large")).click();

System.out.println("************Closed Third Pop UP************");

driver.switchTo().defaultContent();

List<WebElement> productPrices = driver.findElements(By.cssSelector("span.clip-offr-price "));

System.out.println("Size is "+productPrices.size());

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

{

//System.out.println(productPrices);

String[] price = productPrices.get(i).getText().split(".");

//Rs.1,239

String p = price[1].replace(",", "");

int actualPrice =Integer.parseInt(p);

System.out.println("Actual Price of Study lamp is "+actualPrice);

}
}
}

输出

在端口 5562 上启动 ChromeDriver 77.0.3865.40 (f484704e052e0b556f8030b65b953dce96503217-refs/branch-heads/3865@{#442})

只允许本地连接。

请保护 ChromeDriver 和相关测试框架使用的端口,防止恶意代码访问。

2019 年 11 月 18 日下午 5:04:27 org.openqa.selenium.remote.ProtocolHandshake createSession

信息:检测到的方言:W3C

************关闭第一个弹出窗口************

************研究灯被搜索************

************关闭第三个弹出窗口************

尺寸为 48

线程“主”java.lang.ArrayIndexOutOfBoundsException 中的异常:1

在 com.Ecommerce.PageLaunch.main(PageLaunch.java:54)

标签: selenium

解决方案


这是一个特殊的角色。您需要按如下方式拆分。

String[] price = productPrices.get(i).getText().split("\\.");

我运行了您的代码,它正在处理上述代码更改。


推荐阅读