I am working with appium automation on a native android app
I have a MobileElement that I want to get the text from
MobileElement likesElement = (MobileElement) driver.findElement(By.id("com.instagram.android:id/row_feed_textview_likes"));
And then I have the String
String likesVal = likesElement.getText();
When I run System.out.println(likesVal);
it prints:
Liked by you and 107 others
I need to delete everything but 107
so I can run parseInt on likesVal
I have tried
likesVal= likesVal.replaceAll("[*a-zA-Z_.-]", "");
likesVal= likesVal.replaceAll(" ", "");
StringUtils.deleteWhitespace(likesVal);
int likes = Integer.parseInt(likesVal);
System.out.println(likes);
But I am getting a java.lang.NumberFormatException For input string: "107 "
on int likes = Integer.parseInt(likesVal);
After stripping likesVal
of everything but numbers, why wasnt the space that comes before "others" in likesVal
deleted?
After stripping likesVal
of everything but numbers I need "107" to be printed instead of "107 "
I suggest using a pattern to replace all non-digits (\\D
). Like,
String tmp = "Liked by you and 107 others";
// String likesVal = likesElement.getText().replaceAll("\\D+", "");
String likesVal = tmp.replaceAll("\\D+", "");
int likes = Integer.parseInt(likesVal);
System.out.println(likes);
Outputs
107
as requested.