检查网页上的值在 selenium webdriver 中是否为数字

Check that a value on a webpage is numerical in selenium webdriver

当检查网页上是否给出价格时,我不想检查确切的值(因为它可能会发生变化),我首先想检查页面对象是否存在(没有错误,等)然后它返回一个数值。

这可能吗?

使用 C#

private IWebElement priceElement = driver.FindElement(By.Id("price_value"));

public bool PriceObjectValidation()
{
    decimal outDecim;

    try
    {
        string str = priceElement.Text;
        bool isDecimal = decimal.TryParse(str, out outDecim);

        return isDecimal;

    }
    catch (NoSuchElementException e)
    {
        throw new Exception("Price element is not found");
    }
    catch (FormatException)
    {
        return false;
    }
}

在您的测试脚本中,您可以使用

Assert.True(PriceObjectValidation(), "Price element is not numeric value");