在foreach循环中,我捕捉元素的一段文本(如果显示的话)。不过,我只想抓取元素文本中的数字(包括正数和负数)。如何从硒文本中获取数字?
目前的文本可以读取:
+156 on same day
or
-88 on same day
我只想抓住从这段文字的值,所以这应该要么是:
156
or
-88
我怎样才能抓住唯一的数字,如果减去alternativeAirportPrice.Text中的负号?
public string GetAlternativeAirportPrice(By airportPriceLocator)
{
var alternativeAirportPrices = _driver.FindElements(airportPriceLocator);
foreach (var alternativeAirportPrice in alternativeAirportPrices)
{
if (alternativeAirportPrice.Displayed)
return alternativeAirportPrice.Text;
}
return null;
}
有很多方法可以做到这一点..但你可以使用一些内置的扩展方法,如'Char.IsNumeric'在网上做一个快速的谷歌搜索示例 – MethodMan
这不是一个硒问题因为它是一个字符串解析问题。寻找这可能有助于你的研究。 – mrfreester
[Regex for numbers only]可能的重复(https://stackoverflow.com/questions/273141/regex-for-numbers-only) –