selenium - 尝试在 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)
解决方案
这是一个特殊的角色。您需要按如下方式拆分。
String[] price = productPrices.get(i).getText().split("\\.");
我运行了您的代码,它正在处理上述代码更改。
推荐阅读
- php - 仅选择数组 1 中的值并存在于数组 2 中
- laravel - 有人可以在尝试创建自定义工具时解释这个错误吗?
- python - 使用 Altair 在堆积面积图中堆积文本
- apache-spark - 如何使用 Python 对象而不是 Java 对象打印数据框的模式?
- javascript - 如何将 ViewData 值分配给 JavaScript 变量?
- c++ - 如何在 QT 中使用 QFileDialog 打开特定目录
- javascript - Bearer 用于身份验证令牌,但用于不同的令牌?
- python - 如何绘制图像,以便将我的图像用作画笔
- python - 如何在情节中将悬停数据转换为货币?
- ios - Swift:在水平线上垂直堆叠文本