
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
How to set a cookie to a specific domain in selenium webdriver with Python?
We can set a cookie to a specific domain in Selenium webdriver with Python. A cookie is used to hold information sent by the browser. A key−value pair format is utilized and it is like a message provided to the browser by the server.
For cookie addition, the method add_cookie is used. The key and value are passed as parameters to the method. To get back all the cookies, get_cookies method is used. To get a specific cookie, the method get_cookie is used.
To delete cookies, the method delete_all_cookies is used.
Syntax
driver.add_cookie({"Automation": "QA"}); c= driver.get_cookies(); driver.get_cookie({"Automation"); driver.delete_all_cookies();
Example
from selenium import webdriver #set geckodriver.exe path driver = webdriver.Firefox(executable_path="C:\geckodriver.exe") driver.maximize_window() #launch URL driver.get("https://www.tutorialspoint.com/index.htm") #add cookie c = {'name' : "Automation", 'value' : 'QA'} driver.add_cookie(c); #count total cookies print(len(driver.get_cookies())) #obtain cookie with name print(driver.get_cookie("Automation")) #delete cookies driver.delete_all_cookies(); #check cookies after delete d = driver.get_cookies() print("Cookie count after all deletion") print(len(d)) #close browser driver.quit()
Output
- Related Articles
- How to scroll a specific DIV using Selenium WebDriver with Java?
- How to set a cookie and get a cookie with JavaScript?
- How to set Selenium Python WebDriver default timeout?
- How can I close a specific window using Selenium WebDriver with Java?
- How to Select date from a datepicker with Selenium Webdriver using Python?
- How to set window size using phantomjs and selenium webdriver in Python?
- Running Selenium Webdriver with a proxy in Python.
- How to take partial screenshot with Selenium WebDriver in python?
- How to refresh a webpage using Python Selenium Webdriver?
- How install Selenium Webdriver with Python?
- How to set Proxy in Firefox using Selenium WebDriver?
- How to click on a link using Selenium webdriver in Python.
- How to get selected option using Selenium WebDriver with Python?
- How to serialize a cookie name-value pair into a Set Cookie header string in JavaScript?
- How to record a video in Selenium webdriver?

Advertisements