How to run Xvfb with xvfbwrapper on AWS EC2 to records screen of selenium headless session

Question:

I have a problem running selenium sesion with Xvfb to record video file with sesion. Below is my session and wrapper

from selenium import webdriver
from xvfbwrapper import Xvfb

class TestPages(unittest.TestCase):

    def setUp(self):
        self.xvfb = Xvfb(width=1280, height=720)
        self.addCleanup(self.xvfb.stop)
        self.xvfb.start()
        self.browser = webdriver.Chrome()
        self.addCleanup(self.browser.quit)

    def testUbuntuHomepage(self):
        self.browser.get('http://www.ubuntu.com')
        self.assertIn('Ubuntu', self.browser.title)

    def testGoogleHomepage(self):
        self.browser.get('http://www.google.com')
        self.assertIn('Google', self.browser.title)


if __name__ == '__main__':
    unittest.main()

Sesions exit with no errors and enter image description here

The problem is dosent create any kind of files in main directory or /temp directory

enter image description here

where are the files ?

Asked By: PiotrK

||

Answers:

After very long research of multiple way to run this on AWS came to few concussions:

  1. Running on AWS Linux is way too complicated, definitely recommend AWS Ubuntu EC2 for this. This is due to lack of information and some libraries not compatible (scrot, issues with DISPLAY).
  2. The problem at some point is FFMPEG killing all memory and causing disconnecting ssh and other issues.

Here is my final solution, how to record video of what is happening inside Selenium on AWS EC Ubuntu.

    from selenium import webdriver
    import time
    import os 
    import subprocess
    
    from pyvirtualdisplay.smartdisplay import SmartDisplay
    with SmartDisplay() as disp:
          print(os.environ.get("DISPLAY"))
          print ("before webdriver")
          driver = webdriver.Firefox()
          subprocess.call(['ffmpeg -y -video_size 1024x768 -rtbufsize 500M -framerate 24 -f x11grab -i :0.0 -preset ultrafast output.mp4 &'], shell=True)
          driver.get("YOUR WEBSITE URL")
          # YOUR SELENIUM CODE HERE 
          subprocess.call(["echo q"], shell=True)
    disp.stop()
Answered By: PiotrK
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.