gpt-4.1-2025-04-14· OpenAI
LMArena Human Preference 140K
Using selenium for python, how do we clear cookies for a website ?
- User
Using selenium for python, how do we clear cookies for a website ?
- Assistant
To clear cookies for a website using Selenium in Python, you can use the
delete_all_cookies()method provided by the Selenium WebDriver. This method deletes all cookies for the current domain.Here's a simple example:
from selenium import webdriver # Initialize the WebDriver (e.g., Chrome) driver = webdriver.Chrome() # Open the website driver.get('https://example.com') # Clear all cookies for the current domain driver.delete_all_cookies() # (Optional) Refresh the page to see the effect driver.refresh() # Continue with your automation...Notes:
delete_all_cookies()only removes cookies for the current domain loaded in the browser.- If you want to clear cookies for multiple domains, you need to navigate to each domain and call
delete_all_cookies()for each one.
If you want to delete a specific cookie:
driver.delete_cookie('cookie_name')If you want to get all cookies:
cookies = driver.get_cookies() print(cookies)
Summary:
- Use
driver.delete_all_cookies()to clear all cookies for the current website. - Make sure the driver is on the domain you want to clear cookies for.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.