How to trigger headless test execution in Selenium with Python?


Selenium supports headless execution. In the Chrome browser, the headless execution can be implemented with the help of the ChromeOptions class. We have to create an object of this class and apply the add_arguments method to it. Finally, pass the parameter --headless to this method.

Let us obtain the title - About Careers at Tutorials Point - Tutorialspoint of the page launched in a headless mode −

Example

Code Implementation

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
#object of Options class
c = Options()
#passing headless parameter
c.add_argument("--headless")
#adding headless parameter to webdriver object
driver = webdriver.Chrome(executable_path='../drivers/chromedriver', options=c)
# implicit wait time
driver.implicitly_wait(5)
# url launch
driver.get("https://www.tutorialspoint.com/about/about_careers.htm")
print('Page title: ' + driver.title)
# driver quit
driver.quit()

Output

Updated on: 19-Nov-2021

775 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements