Using Selenium with PhantomJS

If you don’t want to play around with things like CasperJS to do your simple browser automation, it turns out that PhantomJS has WebDriver support, and in turn, Selenium supports it.

To use it via the Python bindings, you’ll need to:

1) Install PhantomJS somewhere
2) Install Selenium from PyPi (or install it with Pip)
3) Run:

from selenium import webdriver

driver = webdriver.PhantomJS(phantom_path)

(phantom_path is the path to the phantomjs binary).

And that’s it! Selenium will spawn a PhantomJS process that it communicates to over WebDriver and you can use the Selenium API to do your automation. You therefore don’t need Selenium Server either.

(CasperJS seems to be faster at driving PhantomJS than Selenium, based on anecdotal evidence of a single Google search, but it’s always nice to have options).

Posted on April 18, 2014 at 8:28 pm by Carlos Corbacho · Permalink
In: Python, Web