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

How to Deal with NoSuchCookieException in Selenium

Imagine you’re at a large party in a big house, and you’ve heard that a special kind of cookie is available in the kitchen. You go to the kitchen to find this specific cookie but realize it is not there when you get there. This situation is similar to encountering a NoSuchCookieException in Selenium.

In Selenium, when you try to use a cookie but it cannot be found in the browser right now, this is called NoSuchCookieException.

This particular case belongs to the Selenium WebDriver API, which people use to make web browsers do things by themselves.

When you work on making websites, you find out about cookies. They keep information on the user’s computer to remember login details or what the user likes.

Selenium scripts usually work with these cookies when we do tests. For example, to check if a cookie is put right or to change the cookie values for trying various situations.

Reasons for NoSuchCookieException

Understanding the context and conditions under which your Selenium script operates can help determine why a NoSuchCookieException might occur. Here are some likely causes for this exception to be thrown.

Cookie does not exist

The most straightforward cause is that the cookie simply does not exist. This could be because it was never set or has been deleted.

Incorrect cookie name

If you are trying to access a cookie by its name, a typo or incorrect name will result in this exception. Cookie names are case-sensitive and must be exact.

Cookie expired

Cookies have expiration dates, and the browser automatically removes an expired cookie. It will not be found if you try to access a cookie after its expiration date.

Domain and path mismatch

Cookies are associated with specific domains and paths. If your current page’s domain or path doesn’t match the one the cookie was set for, it won’t be accessible. This is a common issue when working across subdomains or different website sections.

‘Secure’ or ‘HttpOnly’ cookie

Some cookies are marked as ‘Secure’ or ‘HttpOnly’. ‘Secure’ cookies are sent only over HTTPS, while ‘HttpOnly’ cookies are inaccessible by JavaScript, including Selenium scripts. It means that it will not be present if you try to access such a cookie from an insecure network connection or script.

Browser settings

Certain browsers or their specific configurations, like privacy settings, can block/limit cookies.

In case your browser is running with Selenium configured to block cookies, then the cookies might not be set as expected.

Asynchronous nature

Modern web applications may use asynchronous JavaScript operations to set up the cookies. Suppose your Selenium script tries to access a cookie before it has been set through such an operation. In this case, it will fail.

Session cookies and new sessions

Session cookies are deleted when the browser session ends. If your Selenium script starts a new session, previous session cookies will not be available.

Resolution for NoSuchCookieException

Once you have identified the potential cause, it’s time to take steps to fix the problem. You can refer to the below-mentioned tips to address the NoSuchCookieException exception.

Verify cookie existence

Before attempting to access a cookie, check if it actually exists. You can do this by listing all cookies using driver.get_cookies() and checking if your desired cookie is among them.

Remember to check the domain and path

Confirm that the WebDriver is in the right domain and path where the cookie will be placed. Whenever you are on a different domain or path, any cookies for that specific domain or path cannot be obtained.

Wait for the cookie to be set

The asynchronous setting of such cookies could happen when a JavaScript procedure is run after loading the page, which may mean that it may not be available instantly. Selenium has a WebDriverWait mechanism in which explicit waits are used, like expected_conditions, in order to wait for a cookie to be set.

Refresh or navigate again

Sometimes, simply refreshing the page or navigating to it again can resolve timing issues, especially if the cookie is set during the page load process.

Check browser settings

Ensure that the browser’s privacy or security settings aren’t set to block cookies. This is particularly relevant if you are using a custom browser profile or options in Selenium.

Update Selenium and browser drivers

Ensure you are using the latest versions of Selenium and the browser drivers. Compatibility issues can sometimes cause unexpected behavior.

Better and faster alternative to Selenium

Selenium is a well-known tool for automating web tests. Despite that, it has several drawbacks that make the test case development, execution, and maintenance process hard. Alternatively, you can use modern automation testing tools like testRigor due to their ability to tackle these constraints easily.

testRigor is a generative AI-based test automation tool that allows you to automate end-to-end, UI, functional, and system tests across multiple platforms and browsers. This tool does justice to the whole “AI in test automation” hype by making test creation easy. You can write test cases in simple English statements. The command library offers a rich collection of capabilities that make test automation convenient not just for programmers but even those without programming experience.

For example, you can easily interact with UI elements without worrying about their implementation details, like XPaths or CSS.
click on "Sign Up"
You can easily interact with cookies through simple statements like:
get cookie "abc_xyz_111" and save it as "cookie1"
set cookie "cookie1" as "newcookie"

You can explore the rest of testRigor’s offerings here and create a free account to try out these features.

Join the next wave of functional testing now.
A testRigor specialist will walk you through our platform with a custom demo.
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 ...