Search code examples
javastringseleniumselenium-webdrivercompare

How to compare String value to another string after using getText()?


When I extract tooltiptext using getText() method, output comes in three lines:

outside 
temperature
20c

But how to compare extracted 20c to another string 30c?

My approach:

if (tooltiptext.getText() > 30c) {
    System.out.println("temperature is too low outside");
} else {
    System.out.println("temperature is hot outside");
}

Solution

  • This should do it:

    String tooltipText = "outside\ntemperature\n20c";
    // ensure that we parse only the last line, so if digits appear in the other lines, there is no impact
    String toolipTempText = tooltipText.split("\n")[2];
    int tempInCelsius = Integer.parseInt(toolipTempText.replaceAll("[^0-9]", ""));
    if (tempInCelsius > 30) {
      System.out.println("temperature is too low outside");
    } else {
      System.out.println("temperature is too hot outside");
    }