【问题标题】:Get value from website but java code does not work. how to fix?从网站获取价值,但 java 代码不起作用。怎么修?
【发布时间】:2024-05-04 14:15:03
【问题描述】:

我的代码不起作用。它给出了这一行的错误“int temp = Integer.parseInt(currentTemp.substring(0, currentTemp.indexOf("˚ ")));"我尝试了几种方法,但我做不到。也许有不同的因素会影响它。有什么想法可以解决吗? 错误在这里:

背景:#darksky.feature:5 鉴于我在 Darksky 主页 # DarkskySD.iAmOnDarkskyHomePage() 当前温度:43° 当前温度:43˚ 雨。

java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:-1

at java.lang.String.substring(String.java:1967)
at framework.DarkskyTS.etr(DarkskyTS.java:138)
at stepdefinition.DarkskySD.currentTempGreaterOrless(DarkskySD.java:39)
at ✽.Then I verify current temp is not greater or less then temps from daily timeline(darksky.feature:25)

@currenttempgreaterorless 场景:验证当前温度不应高于或低于每日时间线中的温度 #darksky.feature:24 然后我验证当前温度不大于或小于每日时间线中的温度 #DarkskySD.currentTempGreaterOrless() java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:-1 在 java.lang.String.substring(String.java:1967) 在 framework.DarkskyTS.etr(DarkskyTS.java:138) 在 stepdefinition.DarkskySD.currentTempGreaterOrless(DarkskySD.java:39) 在✽。然后我验证当前温度不大于或小于每日时间线的温度(darksky.feature:25)

失败的场景: darksky.feature:24 # 场景:验证当前温度不应高于或低于每日时间线的温度

1 个场景(1 个失败) 2 个步骤(1 个失败,1 个通过) 0m5.234s 公共无效 tempValue(){

    String currentTemp = SharedSD.getDriver().findElement(By.cssSelector(".summary.swap")).getText();
    System. out.println("Current Temp:" + currentTemp);
    List<WebElement> tempsInTimeLine = SharedSD.getDriver().findElements(By.cssSelector(".temps span:last-child"));
    int temp = Integer.parseInt(currentTemp.substring(0, currentTemp.indexOf("˚ ")));
    int highestInTimeLine = temp;
    int lowestInTimeLine = temp;
    for (WebElement tempInTime: tempsInTimeLine) {
        String sLIneTemp = tempInTime.getText();
        int lineTemp = Integer.parseInt(sLIneTemp.substring(0, sLIneTemp.indexOf("˚ ")));
        if (lineTemp > highestInTimeLine){
            highestInTimeLine  = lineTemp;
        }
        if (lineTemp < lowestInTimeLine ){
            lowestInTimeLine = lineTemp;
        }
        //int lineTemp = Integer.parseInt(sLIneTemp.substring(0, sLIneTemp.indexOf("˚ ")));
    }

    System. out.println("Highest Temp:" + highestInTimeLine);
    System. out.println("Lowest Temp:" + lowestInTimeLine );
}

【问题讨论】:

  • 能否将完整的错误文本添加到您的问题中?
  • 你为什么要使用 .indexOf("˚ ") ?为什么你不能总是使用 .indexOf("˚") ?度数符号后总是有空格吗?假设当前温度 String currentTemp = "-10˚";在这种情况下,度数符号后没有空格,异常消息将为Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -1。所以很可能,如果你能确保你的学位符号后面总是有一个空格,那么你的程序可能会工作。

标签: java selenium automation cucumber


【解决方案1】:

不要使用 indexOf("°"),而是尝试使用 indexOf("°")。它会起作用的。您在度数符号后放置的空格是不必要的。

【讨论】:

  • 正如我提到的,我做了同样的事情,但它没有用。我认为问题是不同的。现在,我从网站获取值作为字符串,然后验证这些我需要用作整数。问题就在这里。 Tnx
  • 您的 sLineTemp 是多少?我的意思是在这行 String sLIneTemp = tempInTime.getText(); 之后,sLineTemp 的值是多少。这似乎是真正的罪魁祸首。
【解决方案2】:

该消息告诉您在您尝试解析的字符串中找不到"˚ "。像@Ashish 一样,我怀疑这是由学位符号后的空格引起的。您的新行应如下所示:

int lineTemp = Integer.parseInt(sLIneTemp.substring(0, sLIneTemp.indexOf("˚")));

【讨论】:

  • currentTemp.substring(0, currentTemp.indexOf("˚") - 1) 的问题是它不会包含度数符号之前的字符。如果温度的值为43°,则结果将仅为4,因为根据子字符串方法描述public String substring(int beginIndex, int endIndex) Returns a new string that is a substring of this string. The substring begins at the specified beginIndex and extends to the character at index endIndex - 1. Thus the length of the substring is endIndex-beginIndex. Examples:
  • 你是对的;我从我的回答中删除了该建议。
  • 感谢您的 cmets。我做了你说的所有选择。不幸的是,它没有用。这 lineint lineTemp = Integer.parseInt(sLIneTemp.substring(0, sLIneTemp.indexOf("˚. 给出错误,我在这个页面中添加了
  • @BobLerry 您的生产线到底是什么样的?应该是int lineTemp = Integer.parseInt(sLIneTemp.substring(0, sLIneTemp.indexOf("˚")));
  • 您的 sLineTemp 是多少?我的意思是在String sLIneTemp = tempInTime.getText();这一行之后,sLineTemp 的值是多少。这似乎是真正的罪魁祸首。
【解决方案3】:

这里是回答问题:

driver.get("https://darksky.net/forecast/40.7127,-74.0059/us12/en");
String currentTemp =          driver.findElement(By.cssSelector(".summary.swap")).getText();
System.out.println("Current Temp:" + currentTemp);
List<WebElement> tempsInTimeLine = driver.findElements(By.cssSelector(".temps  span:last-child"));
int temp = Integer.parseInt(currentTemp.substring(0, 2));
int highestInTimeLine = temp;
int lowestInTimeLine = temp;
for (WebElement tempInTime: tempsInTimeLine) {
String sLIneTemp = tempInTime.getText();
int lineTemp = Integer.parseInt(sLIneTemp.substring(0, 2));
if (lineTemp > highestInTimeLine){
    highestInTimeLine  = lineTemp;
}
if (lineTemp < lowestInTimeLine ){
    lowestInTimeLine = lineTemp;
}

}

System.out.println("Highest Temp:" + Integer.toString(highestInTimeLine));
System.out.println("Lowest Temp:" + Integer.toString(lowestInTimeLine ));

【讨论】:

    【解决方案4】:

    我遇到了同样的问题。我不确定我的环境是否存在问题。

    当我调试indexOf("°") 调用时,我看到它认为参数是"°" 而不仅仅是"°"。我将其更改为 '°' 并且有效。

    但是,在运行 mvn clean install 时,我注意到了一个警告 - [WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!

    所以文件是 UTF-8(不确定它是否有 BOM)并且被 Maven 误解了。

    转到他们的常见问题解答,他们建议添加 - &lt;project.build.sourceEncoding&gt;UTF-8&lt;/project.build.sourceEncoding&gt;&lt;properties&gt;下。

    我添加了它,运行 git checkout -- path/to/file.java 以获取原始代码/编码并重新运行 mvn clean install。 警告不再发出,问题已解决。

    【讨论】:

      最近更新 更多