You may have issues depending on what codecs and other elements of gstreamer you have available on your system but this worked for me:
gst-launch-1.0 ximagesrc startx=0 use-damage=0 ! video/x-raw,framerate=30/1 ! videoscale method=0 ! video/x-raw,width=1280,height=1080 ! videoconvert ! x264enc ! avimux ! filesink location=output2.avi
So the videoconvert is required because x264enc has a different input type to ximagesink. x264enc is the codec itself, avimux puts the compressed video in an avi container and filesink is used to write the file out.
For the Python API, an easy MCVE is:
#!/usr/bin/python
import os
import gi
import sys
import time
gi.require_version('Gtk', '3.0')
gi.require_version('Gst', '1.0')
from gi.repository import Gst, GObject, Gtk
Gtk.init(sys.argv)
# initialize GStreamer
Gst.init(sys.argv)
pipeline = Gst.parse_launch ("ximagesrc startx=0 use-damage=0 ! video/x-raw,framerate=30/1 ! videoscale method=0 ! video/x-raw,width=1280,height=1080 ! videoconvert ! x264enc ! avimux ! filesink location=output4.avi")
pipeline.set_state(Gst.State.PLAYING)
time.sleep(15)
pipeline.set_state(Gst.State.NULL)