Run Selenium in Chrome invisibly (headless)

Asked

Viewed 4,368 times

2

I’m doing a program in Python 3.8 using Selenium and Chromedrive, to access a website through Google Chrome. When it is time to run, the program opens two windows, one of Google Chrome and another prompt. However, I want this access to be done invisibly, without open any windows other than Python.

My code goes something like this:

from selenium import webdriver

driver = webdriver.Chrome()

driver.get("https://google.com")

How do I make the process invisible to the user? If I have to install a library, which Python folder do I put it in? What is the command?

The program will run on windows 32.

Thanks in advance, I really need this help.

1 answer

2

To not display the browser, you need to create a ChromeOptions and add the argument --headless.

You will not need to install any library because the ChromeOptions is in the webdriver of selenium, library you are already using.


Creating the ChromeOptions and the option headless:

options = webdriver.ChromeOptions()
options.add_argument("--headless")

Now with the variable options ready, you need to send it the moment you instantiate the Chrome driver:

driver = webdriver.Chrome(chrome_options=options)

Your code will look like this:

from selenium import webdriver

options = webdriver.ChromeOptions()
options.add_argument("--headless")

driver = webdriver.Chrome(chrome_options=options)
driver.get("https://google.com")

#Restante do código

driver.quit()
  • Thank you very much Daniel, you saved my day!!! I did that and it worked out fine

Browser other questions tagged

You are not signed in. Login or sign up in order to post.