Java Selenium 将 getText 转换为整数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21184218/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 07:10:07  来源:igfitidea点击:

Selenium Convert getText to integer

javaseleniumselenium-webdriver

提问by testndtv

I am using the following method to parse some text on the page

我正在使用以下方法来解析页面上的一些文本

getNumberText().getText()

and want to do a assert/comparison using greaterThanOrEqualTo

并希望使用greaterThanOrEqualTo 进行断言/比较

So How do I convert the getText() result to integer value for comparison?

那么如何将 getText() 结果转换为整数值进行比较?

回答by Vinay

You need to Integer class to do this. Integer.parseInt(yourStringNumber). If the value is double then Double.parseInt(yourStringDouble).

你需要 Integer 类来做到这一点。Integer.parseInt(yourStringNumber)。如果值为 double,则 Double.parseInt(yourStringDouble)。

回答by Petr Mensik

int number = Integer.parseInt(getNumberText().getText());

int number = Integer.parseInt(getNumberText().getText());

In case that String is not parseable (thus not integer), NumberFormatExceptionis thrown.

如果 String 不可解析(因此不是整数),则抛出NumberFormatException

回答by Major

public int getNumberText(elem)
{
    return int.Parse(elem.Text());
}

element = driver.FindElement(By.Id("something"));

actual_result = getNumberText(element)

if (actual_result == expected_result)
{
   // do something
}

回答by Kovacic

You could do the following, from this:

您可以从这里执行以下操作:

getNumberText().getText()

... create as one of answers was another method, which will return desired object in Your case int:

...作为答案之一创建是另一种方法,它将在您的情况下返回所需的对象int:

public int getNumber(){
    return Integer.parseInt(getNumberText().getText());
}

so usage would be:

所以用法是:

Assert.assertTrue(getNumber(), intNumber);

Hope this helps,

希望这可以帮助,