Search code examples
javaseleniumtestingautomated-testsui-testing

How to remove first line of element


Im having a small issue when writing my tests. Currently an element I am trying to use is returning me Points:- when I print it on the console. In the html its in the format Points: and on the next line -. How can I remove the "Points: " from this element so it only returns - and also assign the value 0 to it if its - (dash)?

My previous code is

Integer point_player = Integer.parseInt(points_player);
System.out.println(point_player);

And that used to only return a String from 0-9 which I could just convert to integer but now the -(dash) has been introduced.


Solution

  • To strip a prefix, use String.substring(index):

    points_player = points_player.substring(7); // Strip "Points:"
    

    Now spaces might be left:

    points_player = points_player.trim(); // Strip whitespace
    

    Lastly, you need to convert to int in the correct way:

    int value;
    if ("-".equals(points_player)) {
        value = 0; // No points, yet
    } else {
        value = Integer.parseInt(points_player);
    }
    
    System.out.println(value);