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.
Browser Automation with Python Selenium
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.
To use the PhantomJS webdriver, all you need to do is change it to PhantomJS().
Example PhantomJS code:
from selenium import webdriver
After importing the selenium module, we create the webdriver object using
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:
On Windows, the path should be changed to the location of your phantomjs installation.
On Mac or Linux, simply create the driver using: