Home > other >  How does one implement the headless option in the Selenium 4 WebDriver-Manager?
How does one implement the headless option in the Selenium 4 WebDriver-Manager?

Time:10-03

I have but one hurdle to overcome before I can truly call my first bot complete and that is to figure out where to put the options class(?) in order to run ChromeDriverManager in headless mode, and so it stops opening chrome instances! The way the driver is called is:

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from webdriver_manager.chrome import ChromeDriverManager
    options = Options()
    options.headless = True
    driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))

Since the old method of calling webdriver by path hasn't been entirely deprecated yet I don't think there have been very many questions pertaining to the new webdriver-manager. I've found only one or two methods that didn't work, like adding ,options=options after .install() or .options somewhere in the mix. In any case, any suggestions would be appreciated.

CodePudding user response:

try this:

from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager

options = webdriver.ChromeOptions()
options.add_argument("--headless")
driver = webdriver.Chrome(ChromeDriverManager().install(), options=options)

CodePudding user response:

I typed out this comment and never finished it, so my apologies. The correct code to run selenium 4 WebDriver-Manager in headless mode is indeed:

    options = Options()
    options.headless = True                                                  #
    driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()),options=options)

#as opposed to what I was trying:                                            #
    driver = webdriver.Chrome(service=Service(ChromeDriverManager().install(),options=options))

I imagine that 'options' just needs to be a direct argument of webdriver.Chrome(), so I think this should also work:

    driver = webdriver.Chrome(service=Service,options=options(ChromeDriverManager().install()))

Also, I think I have figured out that headless mode makes it easier for websites to mark you as a bot and prompt you to do captchas as after some time of running, because of either captchas or an error in a change of code, my bot couldn't use the search function while headless was true, but performed perfectly with it disabled.

  • Related