Search code examples
pythongoogle-chromeselenium-webdriverbrowsergoogle-chrome-headless

Headless option not working selenium python


Here is the code:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.options import DesiredCapabilities
PATH='C:\Coding_projects\chromedriver.exe'
options = Options()
driver=webdriver.Chrome(PATH,options=options)
def open_ouedkniss():
    
    options.add_argument('headless')
    driver.get("https://www.ouedkniss.com/")
    ad_button=driver.find_element_by_id('header_interstitiel_exit')
    ad_button.click()
    search_bar=driver.find_element_by_id('menu_recherche_query')
    search_bar.click()
    search_bar.send_keys('golf 6')
    search_bar.send_keys(Keys.RETURN)
    
open_ouedkniss()

When I run the code everything works fine but the browser window still opens even with the headless option can someone tell me why?


Solution

  • Try replacing this line in your code

    options.add_argument('headless')

    with

    options.add_argument('--headless')

    Also write above line after options = Options() code.