python logo

phantomjs python

Python hosting: Host, run, and code Python in the cloud!

PhantomJS is a headless browser that can be used with the Selenium web automation module. Unlike the FirefoxDriver or ChromeDriver, the browser stays totally invisible during the process.

It behaves just like the other browsers. To design the process, you can change the webdriver to ChromeDriver or FirefoxDriver and change it to PhantomJS once its working.

Related course
Browser Automation with Python Selenium

Install PhantomJS

Like the other web drivers, the PhantomJS driver needs to be installed.
On Apple Mac you can install PhantomJS with the line:

brew install phantomjs

On Microsoft Windows or Linux, get PhantomJS from the official site.

Selenium PhantomJS

To use the PhantomJS webdriver, all you need to do is change it to PhantomJS().
Example PhantomJS code:

from selenium import webdriver
import time

driver = webdriver.PhantomJS()

html = driver.page_source

After importing the selenium module, we create the webdriver object using

driver = webdriver.PhantomJS()

Then will This will work with both Python 2.7 and Python 3.

If you are on a Windows machine you can specify the path to the phantomjs executable:

driver = webdriver.PhantomJS("C://phantomjs.exe")

On Windows, the path should be changed to the location of your phantomjs installation.

On Mac or Linux, simply create the driver using:

driver = webdriver.PhantomJS()

Download Selenium Examples


Leave a Reply: