简体   繁体   中英

C# Selenium WebDriver - How to get pure text with FindElement.(By.XPath).Text

I have a problem with FindElement(By.XPath).Text . Result from this element is text:

2019-01-01 01:01:01.

Do not know how to get pure text without "-" , spaces , ":" . The plan is to get pure text formatted to:

20190101010101

I've made it to DateTime.Now using .ToString("yyyyMMddHHmm") and it works (That's probably because it is already formatted to DateTime).

Is there any easy way to get .Text formatted to pure text without special characters?

I would suggest to use C# Regular expressions:

Regex.Replace(FindElement(By.XPath).Text, @"[\s\-:]*", "");

My regex above will allow you to select all the spaces , - and : and replace them with empty string in other words remove them from your string.

Regex.Replace(FindElement(By.XPath).Text, @"\s+", "");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM