1

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?

3 Answers 3

3

Try replacing this line in your code

options.add_argument('headless')

with

options.add_argument('--headless')

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

Sign up to request clarification or add additional context in comments.

Comments

0

Going by your question, it looks like you're trying to search for a specific term in the web page, using the search option. I don't think there is a need for you to use RETURN key to press RETURN unless you want to test this functionality specifically.

Using the Selenium 4 RC-1 candidate ( you can install this using pip install selenium==4.0.0.rc1 ), I was able to achieve this via this piece of code -

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
options = Options()
options.add_argument('--headless')
svc=Service(ChromeDriverManager().install())
driver = webdriver.Chrome(service=svc,options=options)
driver.set_window_size(1400,900)
driver.get("https://www.ouedkniss.com/")
wait = WebDriverWait(driver,30)
wait.until(EC.visibility_of_element_located((By.ID,'menu_recherche_query')))

search_bar = driver.find_element(By.ID,'menu_recherche_query')
search_bar.click()
search_bar.send_keys('golf 6')
search_button = driver.find_element(By.ID,'menu_recherche_submit')
search_button.click()
driver.save_screenshot('headfull.png')


driver.quit()

Comments

0

You better use --headless=new because --headless uses old headless mode according Headless is Going Away!:

options.add_argument('--headless=new')

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.