Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python, PhantomJS says I am not using headless?

Tags:

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?

like image 484
Ogden Avatar asked May 18 '18 17:05

Ogden


People also ask

Is PhantomJS headless?

PhantomJS is a discontinued headless browser used for automating web page interaction. PhantomJS provides a JavaScript API enabling automated navigation, screenshots, user behavior and assertions making it a common tool used to run browser-based unit tests in a headless system like a continuous integration environment.

What is headless mode in python?

A headless browser is a web browser without a user interface, it means the browser is running in the background (invisbile). This is great if you want to start a web browser to do tasks, but you don't want or need to see it. You can use any Web Browser like Firefox or Chrome in a headless mode.

How do you execute using headless mode?

You can run Google Chrome in headless mode simply by setting the headless property of the chromeOptions object to True. Or, you can use the add_argument() method of the chromeOptions object to add the –headless command-line argument to run Google Chrome in headless mode using the Selenium Chrome web driver.


2 Answers

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)

like image 128
MagTun Avatar answered Sep 19 '22 17:09

MagTun


In Selenium 3.8.1 PhantomJS marked as deprecated webdriver and recommend us to use either Chrome or Firefox in headless mode.

like image 36
Sergey Samsonov Avatar answered Sep 21 '22 17:09

Sergey Samsonov