【问题标题】:Error ArrayIndexOutOfBoundsException when try to get price of study lamps on pepperfy.com尝试在 pepperfy.com 上获取学习灯的价格时出现错误 ArrayIndexOutOfBoundsException
【发布时间】:2019-11-19 04:31:10
【问题描述】:

我尝试执行相同的逻辑来从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);

}
}
}

输出

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

只允许本地连接。

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

2019 年 11 月 18 日下午 5:04:27 org.openqa.selenium.remote.ProtocolHandshake 创建会话

信息:检测到的方言:W3C

************Closed First Pop UP************

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

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

尺寸为 48

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

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

【问题讨论】:

    标签: selenium


    【解决方案1】:

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

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

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

    【讨论】:

      【解决方案2】:

      你得到这个是因为第 81 行的价格字符串数组是空的,并且在第 85 行你试图访问价格的索引 [1]。

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

      你需要把它放在for循环中,这样你才能得到(i)的值

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-04-12
        • 1970-01-01
        • 2021-10-23
        • 2016-11-23
        • 1970-01-01
        • 2020-03-21
        • 2023-02-07
        • 2020-09-01
        相关资源
        最近更新 更多