0

I've got one problem. I can't parse text from IWebElement into dateTime with UTC.

Selenium without any problem takes text from page:

14.02.2022 09:46:12 UTC

And path to element is correct :

 string dateOfAction = Driver.FindElement(By.XPath($"//div[1]//span[1]")).Text;

I would like to parse this string from dateOfAction(with this same UTC format) into date.

 var actionDate = DateTime.ParseExact(dateOfAction, "dd/MM/yyyy HH:mm:ssZ", null).ToUniversalTime();

if try get string dateOfActionI have "14/02/2022 09:46:12 UTC". But if I trying do

var actionDate = DateTime.ParseExact(dateOfAction, "dd/MM/yyyy HH:mm:ssZ", null).ToUniversalTime(); or just try var actionDate = DateTime.Parse(dateOfAction); the result is not as I expect {1/1/0001 12:00:00 AM}

How to correctly parse string dateOfAction to DateTime dd/MM/yyyy HH:mm:ss with UTC like in span above?

Paweluk
  • 11
  • 6

2 Answers2

0

I see you direct issue is solved but please cf. to this answer for more information.

Clemens
  • 588
  • 6
  • 9
0

Thanks for answers!

I've solved problem using

Date from web page:

14.02.2022 09:46:12 UTC

So I've got string from span, and parsed into date like bellow.

string dateOfAction = Driver.FindElement(By.XPath($"//div[{actionCounter}]//span[1]")).Text;                            
var actionDate = DateTime.ParseExact(dateOfAction, "dd/MM/yyyy HH:mm:ss UTC", null).ToUniversalTime(); 

Variable actionDate has value 14.02.2022 09:46:12 UTC.

and it is good solution.

Paweluk
  • 11
  • 6
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Feb 19 '22 at 05:32