Last modified: Nov 15, 2023 By Alexander Williams
Python Selenium: Hide Browser - Examples
Example 1: Use Headless Mode with Chrome
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
# Create Chrome options
chrome_options = Options()
chrome_options.add_argument('--headless')
# Create a Chrome driver with headless mode
driver = webdriver.Chrome(options=chrome_options)
# Your automation code here
# Close the browser
driver.quit()
Example 2: Use Headless Mode with Firefox
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
# Create Firefox options
firefox_options = Options()
firefox_options.add_argument('--headless')
# Create a Firefox driver with headless mode
driver = webdriver.Firefox(options=firefox_options)
# Your automation code here
# Close the browser
driver.quit()
Example 3: Use PhantomJS (Deprecated)
Note: PhantomJS is deprecated, and its usage is discouraged. Consider using headless Chrome or Firefox instead.
from selenium import webdriver
# Create a PhantomJS driver
driver = webdriver.PhantomJS()
# Your automation code here
# Close the browser
driver.quit()