Open In App

How do I Pass Options to the Selenium Chrome Driver using Python?

Last Updated : 07 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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.

  1. headless: It is used for running the script in the background
  2. disable-extensions: It is used for disabling all the extensions while running the script, etc.
  3. 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
 
# 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.

FAQs

1. Is it possible to modify the behavior of the Chrome browser when using Selenium in Python?

Yes, the user can modify the behavior of the Chrome browser by passing options to the Chrome driver in Python. The options let users set preferences, modify the browser’s behavior, and configure specific features as per their needs.

2. Is it feasible to run Chrome in an incognito setting making use of ChromeOptions in Python?

Yes, you can run Chrome in an incognito setting by including the argument — incognito to ChromeOptions. The following command can be used to run Chrome in incognito.

options.add_argument(“–incognito”)

3. Can the user disable infobars using ChromeOptions in Python?

The notification bars specifically designed to give an alert to the user by appearing on the top of the browser are known as Infobars. They primarily carry the message ‘Chrome is being controlled by automated test software’. The following command can be used to disable infobars.

options.add_argument(“–disable-infobars”)



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads