Skip to content
Advertisement

How do I disable Cookies on Selenium Chrome?

I have been looking around stackoverflow and I cannot find a solution to this. The solutions I did find were apparently old.

chrome_options = webdriver.ChromeOptions()
chrome_options.add_experimental_option("prefs", {"profile.default_content_settings.cookies": 2})

driver = webdriver.Chrome(chrome_options=chrome_options)

This is the error I get

Traceback (most recent call last):
  File "C:UsersameteDocumentsPythonCodeWeb test.py", line 10, in <module>
    driver = webdriver.Chrome(chrome_options=chrome_options)
  File "C:UsersameteAppDataLocalProgramsPythonPython39libsite-packagesseleniumwebdriverchromewebdriver.py", line 73, in __init__
    self.service.start()
  File "C:UsersameteAppDataLocalProgramsPythonPython39libsite-packagesseleniumwebdrivercommonservice.py", line 81, in start
    raise WebDriverException(
selenium.common.exceptions.WebDriverException: Message: 'chromedriver' executable needs to be in PATH. Please see https://sites.google.com/a/chromium.org/chromedriver/home


Process finished with exit code 1

My code:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
PATH = r"C:UsersameteDocumentschromedriver.exe"
driver = webdriver.Chrome(PATH)

chrome_options = webdriver.ChromeOptions()
chrome_options.add_experimental_option("prefs", {"profile.default_content_settings.cookies": 2})

driver = webdriver.Chrome(chrome_options=chrome_options)

driver.get("https://www.google.com/")
print (driver.title)

search = driver.find_element_by_id("input")
search.send_keys("One Piece")
search.send_keys(Keys.RETURN)

time.sleep(5)
driver.quit()

Advertisement

Answer

Everything looks okay, but the error says chromedriver' executable needs to be in PATH , right ?

Which means instead of :

driver = webdriver.Chrome(chrome_options=chrome_options)

you need to do this :

driver = webdriver.Chrome(executable_path = PATH, chrome_options=chrome_options)

should work for you.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement