gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 02:33:26 25 4
gpt4 key购买 nike

我想要比较 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);
}
}
driver.close();
System.setProperty("webdriver.chrome.driver","C:\\Selenium-java\\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("//span[@class='a-size-medium a-color-base a-text-normal']"));
List< WebElement> phoneprice1 = driver1.findElements(By.xpath("//span[@class='a-offscreen']"));
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() };
System.out.println(entriesArr1 );
System.out.println(entriesArr1.length);
data1.add(entriesArr1);
}
}
driver1.close();

String[] d = getMinPhonePrice(data1);
System.out.print(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;
}
}

当我运行代码时,它显示字符串索引超出范围:-1 int value = Integer.parseInt(data.get(i)[1].substring(1).replace(",", "“));

最佳答案

我在输入点击后添加了一些 sleep 等待加载页面,并且您错误地选择了定位器By.xpath("//span[@class='a-offscreen']"),定位器没有给出任何文本 ="",因此当您使用 .substring(1) 时,会导致错误 String index out of range: -1 ,请尝试以下代码。

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

关于java - 比较两个值时,它显示字符串索引超出范围 : -1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57217914/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com