How to get representation as a writer
The first thing to realise about agents is that they are not a substitute for hard work. It isn't their job to break into the industry for you, or to handle all the boring non-creative stuff while you sit at home and write. As a screenwriter you are an artist AND a business person, and you have to balance both of these (very different) sets of skills. When, and if, you gain representation, an agent can shoulder a lot of your business responsibilities like negotiating fees and contracts, and arranging meetings with producers and production companies. But to begin with you have to do this yourself. You have to be your OWN agent.
That you should write as much as possible goes without saying, but in addition to this you need to make yourself know to the film industry. You do this by attending networking events, talks, film related Q&As, etc., by participating in online forums, and joining filmmaking networks like Shooting People, Stage 32, and others. Eventually you will get to know like-minded filmmakers and hopefully end up collaborating on a short film or two, which will raise your profile; particularly if the films do well in festivals. You also need to keep an eye out for schemes and talent labs like Guiding Lights, Triangle, Think Shoot Distribute, Berlinale, Torino Film Lab, etc.
There are also many screenwriting contests in the US and UK that can get you noticed by agents, as well as managers and producers, and/or help build up your CV.
When you have a few screenplays under your belt and have a solid CV that shows you are both talented and dedicated, then you can realistically expect agents to start taking an interest. Your best bet would probably be to get recommended to an agent by someone in the industry who knows you and your work.
But even with a solid CV, finding the right agent can be a struggle, so you have to stay proactive. When you start to make headway in the industry and start to feel like maybe you don't need an agent... that may be when you finally get one!