Headless Chrome & Firefox

In Previous section, we have discussed about handling cookies in website and how to add, delete, get cookies and print in terminal. Now we will learn about how to automate Headless Chrome & Firefox.

Objectives:

  • What is Headless Browsers?
  • How to run selenium script in Headless Browsers
  • Practical Example

What is Headless Browsers?

Headless browser is nothing but the web browser without any graphical user interface. This is very faster way to automate the web application. As there is not GUI, everything is working through the command line.

Why the Headless Chrome & Firefox is important?

Yes. Headless browsers are very important for testing the web page. It can easily manipulate and understand the DOM of HTML like other browsers including CSS, JS, Ajax, etc.

There are some common uses of headless browser which help industry very rapidly.

  • Testing the web application
  • Simulation of Java Script
  • UI Automation running in background
  • Testing of the JS Libraries
  • Automate the functionalities like click and buttons are very fast
  • Testing the SSL Performance
  • Page response time is very low and Report generation is very easy
  • Screenshot of result

How to run selenium script in Headless Browsers?

From the above section, you have seen that Headless browsers are very easy to handle in Selenium

To achieve this we will use ChromeOption or FirefoxOption in Selenium Python.

from selenium import webdriver
from webdriver_manager.chromeimport ChromeDriverManager


options = webdriver.ChromeOptions()
options.headless = True
driver = webdriver.Chrome(ChromeDriverManager().install(), options = options)

driver.implicitly_wait(10)
driver.get('https://www.reddit.com/')
driver.maximize_window()
print(driver.title)

Output:

Headless Chrome & Firefox

Explanation:

Here to work with Headless, we need to user ChromeOptions() and will make this True for Headless. And we need to pass the object of ChromeOptions() to the WebDriver Manager.

Options = webdriver.ChromeOptions()
options.headless = True

These two lines make your script to run in Headless mode. In Headless mode, while running your script then it will not open the browser. Output will be printed in Terminal. As you have seen in above example that, we have printed the title of the page.

Like Chrome we can work with Headless Firefox also as below.

from selenium import webdriver
from webdriver_manager.chromeimport ChromeDriverManager
from webdriver_manager.firefoximport GeckoDriverManager


options = webdriver.FirefoxOptions()
options.headless = True
driver = webdriver.Firefox(executable_path=GeckoDriverManager().install(), options =  options)
driver1.get('https://www.reddit.com/')

print(driver.title)

Output:

Q. Where Headless browsers are being used in Selenium Python?

We can use Headless browser where we do not need to open the browsers and only we need to verify the output in terminals.

There is another way to run your script in Headless mode is adding “- -headless” as argument in add_argument() method with option class object.

from selenium import webdriver
from webdriver_manager.chromeimport ChromeDriverManager
from webdriver_manager.firefoximport GeckoDriverManager

options = webdriver.ChromeOptions()
options.headless = False
options.add_argument('--headless')
driver = webdriver.Chrome(ChromeDriverManager().install(), options = options)

driver.implicitly_wait(10)
driver.get('https://www.reddit.com/')
driver.maximize_window()
print(driver.title)

Output:

Running Script in Incognito Mode:

Incognito mode in browser is nothing but a private browser where history of the browsers will not be saved. Like Headless, you can also run your script in Incognito Mode by passing the argument as below.

from selenium import webdriver
from webdriver_manager.chromeimport ChromeDriverManager
from webdriver_manager.firefoximport GeckoDriverManager

options = webdriver.ChromeOptions()
options.headless = False
options.add_argument('--incognito')
driver = webdriver.Chrome(ChromeDriverManager().install(), options = options)

driver.implicitly_wait(10)
driver.get('https://www.reddit.com/')
driver.maximize_window()
print(driver.title)

Output:

Summary:

  • Headless browser is nothing but the web browser without any graphical user interface. This is very faster way to automate the web application.
  • To achieve Headless browser we will use ChromeOption or FirefoxOption in Selenium Python.
  • To run your script in Headless mode is adding “- -headless” as argument in add_argument() method with option class object.
  • Incognito mode in browser is nothing but a private browser where history of the browsers will not be saved. Like Headless, you can also run your script in Incognito Mode by passing the argument as below.

Leave a Reply

Your email address will not be published. Required fields are marked *