Python, PhantomJS says I am not using headless

phantomjspythonselenium

my code is:

from selenium import webdriver

driver = webdriver.PhantomJS(executable_path='driver/bin/phantomjs.exe')
driver.get("https://www.test.com")
print(driver.current_url)

It seems to run fine but before it runs I always get this error:

UserWarning: Selenium support for PhantomJS has been deprecated, please use headless versions of Chrome or Firefox instead warnings.warn('Selenium support for PhantomJS has been deprecated, please use headless

Why am I getting this error? I thought my PhantomJS was headless as it still works and no browser pops-up is this error save to ignore?

Best Answer

Selenium considers PhantomJS as deprecated, so you need to us either Chrome or Firefox in headless mode.

Here are the steps to use Chrome in headless mode:

  1. download chrome driver from https://sites.google.com/a/chromium.org/chromedriver/getting-started
  2. extract it to a folder
  3. add this folder to your PATH environment variable (if you don't do it, you will have to use webdriver.Chrome('/your/path/to/chromedriver') in the code below instead of webdriver.Chrome())

Then use it like this:

from selenium import webdriver

# prepare the option for the chrome driver
options = webdriver.ChromeOptions()
options.add_argument('headless')

# start chrome browser
browser = webdriver.Chrome(chrome_options=options)
browser.get('http://www.google.com/xhtml')
print(browser.current_url)
browser.quit()

More on how to use ChromeDriver
For the other options: here (also here and here)