Turn your manual testers into automation experts! Request a DemoStart testRigor Free

Why is UnableToSetCookieException Faced in Selenium?

Exceptions are thrown when Selenium WebDriver faces an error during web browser or element interaction. They help in understanding the error and the reason for its occurrence.

One such typical exception that we see is UnableToSetCookieException. Let us understand why this exception gets triggered and how we can resolve it.

Reasons for UnableToSetCookieException

This exception is thrown when the WebDriver cannot set a cookie for the web page. Cookies are small packets of data that are being sent from the web server to the browser. They mainly contain information about the user’s session or preference. Using Selenium, we can interact with the cookies in our test scripts.

Now let’s see some common reasons why this exception is thrown.

  • Invalid Cookie Name: This occurs if we attempt to set the cookie with an invalid name. Cookies have specific naming conventions, such as there shouldn’t be any special characters or spaces. So, if this is not followed, an exception will be thrown.

  • Invalid Cookie Value: Similar to the cookie name, the error will be thrown if we try to set an invalid cookie value. Cookie value also follows criteria like no spaces and special characters.

  • Cookie Domain Mismatch: Cookies are associated with a specific domain. If we try to set a cookie for a domain that does not match the current web page’s domain, then this exception will be brown. For example, the exception is thrown when we try to set a cookie with domain maindomain.com for the web page with domain subdomain.maindomain.com. Then, the exception is thrown.

  • Cookie Path Mismatch: An exception will be thrown if we try to set a cookie with a different path from the web page.

  • Expired Cookies: When we try to set an expired cookie, the exception occurs.

  • Secured Cookie on Non-Secure Page: The exception will be thrown if we try to set an HTTPS cookie on an HTTP page.

  • Cookie Size Limit Exceeded: Some web pages have limitations on cookie size. So, if we try to set a cookie that has exceeded the allotted size, the exception will be raised.

How to avoid UnableToSetCookieException

To avoid UnableToSetCookieException, we need to understand the common causes of the exception and take proactive steps to prevent them. Let’s review a few common strategies:

Correct Domain Marking

  • We need to be sure that the cookie’s domain matches the current page’s domain in the browser. We cannot set the cookies to browsers that have different domains.
  • If we need to set a cookie for a particular subdomain, we must navigate to that sub-domain in the browser and then set the cookie.

Valid Cookie Attributes

  • Set all the cookie attributes correctly. Mandatory attributes like name and value should be present, not empty or null.
  • If the cookie needs an expiration date, ensure it’s set and shouldn’t be a past date.
  • Make sure the cookie path is set correctly. If the path is not set, it defaults to the path of the current URL.

Secure and HttpOnly Flags

  • If we have a cookie marked “Secure”, then it can be set over a secure HTTPS connection only.
  • Cookies that are marked with “HttpOnly” cannot be set via Selenium, as they can be accessible only through HTTP requests, not through client-side scripts.

Session Management

  • We can set cookies only if there is an active browser session. Check if the browser is open and the page is loaded before we set the cookie.
  • Avoid setting the cookies after the browser session ends; otherwise, it will throw an exception.

Error Handling

Implement proper error handling in the test scripts. So, with the try-catch mechanism, we can catch if there is any UnableToSetCookieException occurring. Let’s see a sample script that handles the exception:
from selenium import webdriver
from selenium.common.exceptions import UnableToSetCookieException

driver = webdriver.Chrome()

try:
  # Attempt to set a cookie here
  driver.get("https://example.com")
  driver.add_cookie({"name": "myCookie", "value": "12345"})
except UnableToSetCookieException as e:
  print(f"Unable to set cookie: {e}")
finally:
  driver.quit()

Using Next-Gen AI Automation tools

While using Selenium, you must handle all the exceptions to avoid script halt during test runs. This is quite time-consuming, and also, if the test code repository is vast, it adds enormous additional effort in debugging. That’s one of the reasons why Selenium got pulled backstage now. With extreme programming methodologies, companies expect automation tools to be more intelligent. That’s why testRigor is a popular choice. With its integrated generative AI, testRigor provides incredible features that make it possible for anyone in your team to write and execute test cases irrespective of their technical knowledge.

Let’s see how testRigor handles cookies.

  • Handling cookies is much easier in testRigor, as we don’t have to write complicated scripts. testRigor supports writing test scripts in parsed English that removes the dependency on programming languages.

  • You can set cookies like: set cookie “cookie value” as “cookie-name”.

    To get a cookie value, use: get a cookie “cookie-name” and save it as “variableName”.

    Now, to clear cookies, just write: clear cookies. You can read more about it here.

Conclusion

The approach to testing using test automation has significantly changed in the current scenario. Testing is more valuable than ever, as every company looks to release their applications early with top-notch quality without any bugs.

Today, using age-old tools creates many hurdles to quick testing, and false positive bugs can not even be considered. With testRigor, get that gap closed. If you consider testRigor’s ROI, it’s much cheaper than the cost incurred on debugging and handling exceptions of these old tools.

If you have not tried testRigor, Sign Up for a free trial to discover its powerful features.

You're 15 Minutes Away From Automated Test Maintenance and Fewer Bugs in Production
Simply fill out your information and create your first test suite in seconds, with AI to help you do it easily and quickly.
Achieve More Than 90% Test Automation
Step by Step Walkthroughs and Help
14 Day Free Trial, Cancel Anytime
“We spent so much time on maintenance when using Selenium, and we spend nearly zero time with maintenance using testRigor.”
Keith Powe VP Of Engineering - IDT
Related Articles

SAP Concur Testing

Concur Technologies, now SAP Concur, is an American software-as-a-service (SaaS) company providing travel and expense management ...

Authorize.net Testing

You need a payment gateway in several scenarios, especially when you are running a business that requires secure, efficient, and ...

KYC Portal CLM Testing

KYC Portal CLM is an award-winning Client Lifecycle Management (CLM) platform designed to streamline and automate the Know Your ...
On our website, we utilize cookies to ensure that your browsing experience is tailored to your preferences and needs. By clicking "Accept," you agree to the use of all cookies. Learn more.
Cookie settings
Privacy Overview
This site utilizes cookies to enhance your browsing experience. Among these, essential cookies are stored on your browser as they are necessary for ...
Read more
Strictly Necessary CookiesAlways Enabled
Essential cookies are crucial for the proper functioning and security of the website.
Non-NecessaryEnabled
Cookies that are not essential for the website's functionality but are employed to gather additional data. You can choose to opt out by using this toggle switch. These cookies gather data for analytics and performance tracking purposes.