Provide a Selenium script demonstrating how to run a browser in headless mode, specifically using Chrome
# Import necessary libraries
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
# Create ChromeOptions and add the "headless" argument to run Chrome in headless mode
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("headless")
# Create a Service object for the Chrome browser
service_obj = Service()
# Initialize the Chrome WebDriver with the created Service and options
driver = webdriver.Chrome(service=service_obj, options=chrome_options)
# Open the specified website
driver.get("https://www.saucedemo.com")
# Set an implicit wait of 3 seconds to allow elements to be found
driver.implicitly_wait(3)
# Maximize the browser window
driver.maximize_window()
# Print the title of the current webpage to the console
print(driver.title)
# Close the browser window
driver.quit()
Comments