Home > Back-end >  How do I check if a WebElement is a positive number in Selenium Java?
How do I check if a WebElement is a positive number in Selenium Java?

Time:03-10

So, I have a WebElement and I need to check if this element is a positive number; how do I do this in Java using Selenium?

The only thing I was able to do, so far, is a @FindBy(xpath), but I could not do the Actions part cause I have no idea on how to verify if the element is a positive number.

Thanks in advance.

CodePudding user response:

WebElement myElement = driver.findElementByXpath("theXpath");
// or read the attribute value 
String text = myElement.getAttribute("text");
int number = Integer.parseInt(text);
if (number > 0) {
  // positive
}
else {
  // negative 
}

CodePudding user response:

The answer above should work fine, you may want to handle number format exception in case the attribute is blank.

  • Related