Capturing screenshot of ray traced object from python script

I’m trying to run a python script that renders a mesh by using ray tracing (either OSPRay or OptiX) engine, followed by screen capturing of the rendered window. My problem is that the rendering process takes some time, and the screenshot is being taken before the rendering ends. As a result, the captured image is low quality. What would be the way to solve this?
Many thanks for your help

That does not sound possible. Did you try increasing the number of sample per pixel ?

Many thanks Matheieu,
That solved my problem. I have needed much more samples for the OptiX tracer to get an equivalent quality image compared to the ospray tracer.

Yes, Optix does not support denoising inside paraview yet.