"... while at somewhere."

Hello,
Is the following sentence grammatically correct…

“Pirates steal while at sea.”

Or we have to use this one instead?

“Pirates steal while they are at sea.”

I guess that the first sentence is OK but I’m not sure if it’s safe to use such a structure in TOEFL.

Thank you.

Yes, the first sentence is grammatically OK. I would not use that particular sentence as an example though, as it reads just slightly oddly in terms of its content.

“Many employees waste time on the Internet while at work.”
“He began the business while at university.”

My feeling is that omission of the “be” verb in sentences like these is mostly found in written English and less likely in conversation.

Thank you so much. :‌)

Which is better, the first sentence or the second?.

If you mean the sentences in message #1, both are fine, but as Dozy has explained, this particular example is not good.