How do I Pass Options to the Selenium Chrome Driver using Python?
Last Updated :
24 Apr, 2025
An open-source automation tool that helps in controlling browsers without any human intervention is known as Selenium. It is mostly used for testing purposes. For smoothing the testing through Selenium, we can use options available in Selenium that help in testing in the background, disabling extensions, etc. In this article, we have defined how can we pass options to the Selenium Chrome driver using Python.
Pass Options to the Selenium Chrome Driver using Python
Set-up
1. Install the favorite Python IDE, such as Pycharm.
2. Run the following command to install Selenium.
pip install selenium
3. Download the Selenium ChromeDrivers from the official website of Selenium.
4. Put the Selenium JARs in the system's path environment variable.
5. Now, import the required Selenium libraries, i.e., webdriver and Service.
6. webdriver: A set of methods that are used to control web browsers and interact with the elements on the web page is known as a web driver.
7. Service: A browser driver that helps in initiating the WebDriver instances for controlling any browser is known as a Service.
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
Using the ChromeOptions class
Now, we will state the web driver options. The objects let the web driver customize the behavior of a web browser as known as options. Here, we have stated the ChromeOptions. You need to state the options of the browser in which you are running the script.
options = webdriver.ChromeOptions()
Adding Options
Once we have stated the options, we can pass the arguments to options, such as headless, disable-extensions, and disable-gpu.
- headless: It is used for running the script in the background
- disable-extensions: It is used for disabling all the extensions while running the script, etc.
- Disable: It is used for disabling GPU acceleration.
chrome_options.add_argument("--headless")
chrome_options.add_argument("--disable-extensions")
chrome_options.add_argument("--disable-gpu")
Launching Chrome with Options
Moreover, define the path where the browser driver is present using the Service function. Here, we have used the Chrome Driver path, you can use your favorite browser path too. Then, we will initiate the chrome driver using the chrome driver path.
# Define the chrome driver path
ser = Service("#Chrome_driver_path")
# Initiate the Chromedriver by passing options as argument
driver = webdriver.Chrome(service=ser,options=options)
Additional Options and Resources
1. Obtain the web page: In this step, we will get the URL from the user that he wants to open through Chrome Driver and open the web page.
driver.get("#URL_of_website")
2. Perform the necessary actions: Further, the user can perform the necessary actions, such as finding the element on a web page and clicking it, clicking the button, obtaining the screenshot, etc.
# Finding the element by Id
element = driver.find_element(By.ID ,"#Id_of_element")
# Clicking of element
element.click()
Example
In this example, we have stated the web driver Options and then added a headless argument for running the Python script in the background. Further, we have opened the Geeks For Geeks website (link) and obtained the title of the page.
Python3
# Importing Selenium libraries
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
# State the Webdriver options
options = webdriver.ChromeOptions()
# Add argument to make it headless
options.add_argument('--headless')
# Define the chrome driver path
ser=Service("C:\\Users\\Vinayak Rai\\Downloads\\chromedriver-win64\\chromedriver-win64\\chromedriver.exe")
# Initiate the Chromedriver by passing options as argument
driver = webdriver.Chrome(service=ser,options=options)
# Obtain the web page
driver.get("https://www.geeksforgeeks.org/")
# Print the title of the web page
print(driver.title)
# Close the driver
driver.quit()
Output:
GeeksforGeeks | A computer science portal for geeks
Conclusion
In conclusion, Selenium is a powerful tool that helps in controlling web drivers autonomously. The addition of options in Selenium is like a cherry on the cake, it gives the web driver more features, like running the scripts in the background, disabling all the unnecessary extensions, and disabling the GPU acceleration. I hope after reading the above article, you will able to pass options to the Selenium Chrome driver using Python.
Similar Reads
How do I pass options to the Selenium Chrome driver using java?
Selenium WebDriver is a widely used tool for automating web browsers. When working with Selenium ChromeDriver, passing custom options to the browser allows you to tailor the browsing experience based on your test requirements. Whether you're running tests in headless mode, disabling notifications, o
2 min read
How to disable images in chrome using Selenium java?
Disabling images in Chrome during automated testing can enhance performance and speed up your Selenium tests. This is particularly useful when dealing with large web pages or when you want to focus on specific elements without the distraction of images. In this guide, we'll walk you through how to d
2 min read
How to setup Chrome driver with Selenium java on MacOS?
Automation is now a critical detail of software program development, and Selenium stands proud as one of the leading tools for browser automation. if you plan to use Selenium with Java on macOS, the first step is to install ChromeDriver. ChromeDriver is a critical thing that enables Selenium to talk
3 min read
How to Add Chrome Extension using Python Selenium
IntroductionSelenium is a tool for browser automation that supports multiple browsers for testing. With webdriver we can use multiple languages like Python, C#, and Java to write the code for automation testing. For Adding a Chrome extension in a browser with Selenium WebDriver allows us to automate
5 min read
How to save and load cookies in Selenium using Python
In web automation and testing, maintaining session information across different runs of scripts is often necessary. Cookies are a great way to save session data to avoid logging in repeatedly. Selenium, a popular tool for web testing, provides straightforward ways to save and load cookies using Pyth
4 min read
How to get current_url using Selenium in Python?
While doing work with selenium many URL get opened and redirected in order to keeping track of URL current_url method is used. The current_url method is used to retrieve the URL of the webpage the user is currently accessing. It gives the URL of the current webpage loaded by the driver in selenium.
2 min read
set_window_position driver method - Selenium Python
Seleniumâs Python Module is built to perform automated testing with Python. Selenium Python bindings provides a simple API to write functional/acceptance tests using Selenium WebDriver. To open a webpage using Selenium Python, checkout â Navigating links using get method â Selenium Python. Just bein
2 min read
How to Run Opera Driver in Selenium Using Java?
Selenium is a well-known software used for software testing purposes. Selenium consists of three parts. One is Selenium IDE, one is Selenium Webdriver & the last one is Selenium Grid. Among these Selenium Webdriver is the most important one. Using Webdriver online website testing can be done. Th
3 min read
Get all text of the page using Selenium in Python
As we know Selenium is an automation tool through which we can automate browsers by writing some lines of code. It is compatible with all browsers, Operating systems, and also its program can be written in any programming language such as Python, Java, and many more. Selenium provides a convenient A
3 min read
Capture all the Options in Selenium with Python
Selenium is an effective device for controlling an internet browser through the program. It is purposeful for all browsers, works on all fundamental OS and its scripts are written in numerous languages i.e Python, Java, C#, etc, we will be using Python.Prerequisite: Browser Automation Using Selenium
2 min read