第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

當(dāng)比較兩個(gè)值時(shí),它顯示字符串索引超出范圍:-1

當(dāng)比較兩個(gè)值時(shí),它顯示字符串索引超出范圍:-1

慕的地6264312 2023-04-26 14:09:50
我想要從 flipkart 和亞馬遜網(wǎng)站比較的商品的最低價(jià)格。我正在這兩個(gè)網(wǎng)站上搜索特定的手機(jī)    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);                        }                    }當(dāng)我運(yùn)行我的代碼時(shí),它顯示 String index out of range: -1 in the line int value = Integer.parseInt(data.get(i)[1].substring(1).replace(",", "")) ;
查看完整描述

1 回答

?
桃花長(zhǎng)相依

TA貢獻(xiàn)1860條經(jīng)驗(yàn) 獲得超8個(gè)贊

我添加了一些sleep for wait a load page enter click后,你錯(cuò)誤的選擇了一個(gè)定位器By.xpath("//span[@class='a-offscreen']"),定位器沒(méi)有給出任何文本="",所以當(dāng)你使用.substring(1)它時(shí)會(huì)導(dǎo)致錯(cuò)誤String index out of range: -1,請(qǐng)?jiān)囋囅旅娴拇a。


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);


    //I'm edit here

    Thread.sleep(5000);


    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);

        }

    }

    driver.close();


    System.setProperty("webdriver.chrome.driver","C:\\Seleniumjava\\driver\\chromedriver.exe");

    WebDriver driver1 = new ChromeDriver();


    driver1.manage().timeouts().implicitlyWait(20,TimeUnit.SECONDS);

    //open amazon site

    driver1.get("https:/www.amazon.in");

    driver1.findElement(By.xpath("//input[@id='twotabsearchtextbox']")).sendKeys("Redmi 7 ( 32 MB , 2 GB ) Black");

    driver1.findElement(By.xpath("//input[@id='twotabsearchtextbox']")).sendKeys(Keys.ENTER);


    Thread.sleep(3000);

    List< WebElement> phonenames2 = driver1.findElements(By.xpath("//a//span[@class='a-size-medium a-color-base a-text-normal']"));

    //I'm edit here

    List< WebElement> phoneprice1 = driver1.findElements(By.xpath("//*[@class='a-price' and @data-a-size='l']"));


    for( int j=0;j<phonenames2.size();j++) {

        if(phonenames2.get(j).getText().contains("Redmi 7 (Eclipse Black, 2GB RAM, 32GB Storage)")) {

            entriesArr1 = new String[]{phonenames2.get(j).getText().toString(), phoneprice1.get(j).getText().toString() };

            data1.add(entriesArr1);

        }

    }

    driver1.close();


    String[] d = getMinPhonePrice(data1);

    System.out.println(d[0] + ", " + d[1]);

}


//method for minimum phone price

public static String[] getMinPhonePrice(ArrayList<String[]> data){

    int value_min = Integer.parseInt(data.get(0)[1].substring(1).replace(",", ""));

    String key_min="";


    for(int i=1;i<data.size();i++) {

        int value = Integer.parseInt(data.get(i)[1].substring(1).replace(",", ""));

        if(value < value_min) {

            value_min=value;

            key_min=data.get(i)[0];

        }

    }

    String[] d = {key_min,String.valueOf(value_min)};

    return d;




查看完整回答
反對(duì) 回復(fù) 2023-04-26
  • 1 回答
  • 0 關(guān)注
  • 142 瀏覽
慕課專(zhuān)欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢(xún)優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)