In this tutorial we want to talk about an important concept in Selenium and that is Cookies in Python Selenium, so cookies are an important part of web browsing, and using cookies websites can store user specific information and maintain state across multiple sessions. when we want to automate web interactions using Python Selenium, it is important to know how to handle cookies. In this lesson we want to learn about cookies, and how to handle that with Python Selenium.
What are Cookies ?
Cookies are small pieces of data, and that is stored on the user’s computer by websites they visit. Cookies contain information such as user preferences, session identifiers, and authentication tokens. Cookies are sent to the server with each HTTP request, and it allows websites to personalize content and maintain user sessions.
Cookies consist of key-value pairs and can have different attributes, for example expiration date, domain, and path.
To work the examples of this tutorial you need some requirements, first you should have installed Python in your system, then we need to install Python Selenium and you can use pip for that like this.
1 |
pip install selenium |
Managing Cookies with Python Selenium
First of all we need to import our module from Python Selenium, we need to import web driver from selenium.
1 |
from selenium import webdriver |
After that we need to launch a browser instance. In this example, we are going to use Google Chrome. instantiate a webdriver.Chrome object, which represents the browser window.
1 |
driver = webdriver.Chrome() |
To demonstrate cookie handling, let’s open Google.com and search for something. we want to use get() method of the webdriver object to go to the desired URL.
1 |
driver.get("https://www.google.com") |
Now we want to get cookies from the current browser session, we can use get_cookies() method. it returns a list of dictionaries, where each dictionary represents a cookie.
1 2 |
cookies = driver.get_cookies() print(cookies) |
For adding a new cookie, we use add_cookie() method, which accepts a dictionary containing the cookie details. In this example, let’s add a custom cookie with the name example_cookie and value 12345.
1 2 |
new_cookie = {'name': 'example_cookie', 'value': '12345'} driver.add_cookie(new_cookie) |
After adding the new cookie, we can verify its presence by retrieving the cookies again and checking if the added cookie is present.
1 2 3 4 5 6 7 8 |
cookies = driver.get_cookies() for cookie in cookies: if cookie['name'] == 'example_cookie': print("Added cookie found") break else: print("Added cookie not found") |
If you want to remove a specific cookie, you can use delete_cookie() method and provide the cookie name.
1 |
driver.delete_cookie('example_cookie') |
After that you have finished working with the browser, remember to close it using the close() method.
1 |
driver.close() |
This is the complete code for this article
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
from selenium import webdriver # Launch the browser driver = webdriver.Chrome() # Open Google.com driver.get("https://www.google.com") # Obtain the current cookies cookies = driver.get_cookies() print(cookies) # Add a new cookie new_cookie = {'name': 'example_cookie', 'value': '12345'} driver.add_cookie(new_cookie) # Verify the added cookie cookies = driver.get_cookies() for cookie in cookies: if cookie['name'] == 'example_cookie': print("Added cookie found!") break else: print("Added cookie not found!") # Delete the cookie driver.delete_cookie('example_cookie') # Close the browser driver.close() |
Run the code and this will be the result in the console
FAQs about Cookies in Python Selenium:
Q: What are cookies, and why they are important in web browsing?
A: Cookies are small pieces of data, and it is stored by websites on a user’s computer. They play an important role in maintaining user sessions, storing preferences, and personalizing content based on user behavior.
Q: How can I view cookies in Python Selenium?
A: You can view cookies in Python Selenium by using get_cookies() method, and it retrieves all cookies present in the current session.
Q: Can I add cookies to a Selenium session programmatically?
A: Yes, you can add cookies to Selenium session using add_cookie() method, and it allows you to specify the name, value, and other attributes of the cookie.
Q: Is it possible to delete cookies in Python Selenium?
A: Yes, you can delete specific cookies or all cookies using delete_cookie() and delete_all_cookies() methods, using those methods the cookies will be deleted.
Q: How do I modify existing cookies in Python Selenium?
A: To modify existing cookies, you can first delete the cookie using delete_cookie() method and after that add the updated cookie using add_cookie() method with the new values.
Q: Can I automate cookie management in Selenium scripts?
A: Yes, you can automate cookie management in Selenium scripts by adding logic to add, delete, and modify cookies based on specific requirements or user interactions during the automation process.
Learn More on Python Selenium
- Python Selenium Web Scraping
- Test Automation with Python Selenium
- Python Selenium WebDriver Tutorial
- Browser Interaction in Python Selenium
- Browser Profiles in Python Selenium
- Python Selenium Frames
Subscribe and Get Free Video Courses & Articles in your Email