Best Selenium code snippet using org.openqa.selenium.InvalidCookieDomainException
org.openqa.selenium.InvalidCookieDomainExceptionThe WebDriver error - invalid cookie domain error, occurs if a cookie is illegally attempted to be set under a different domain from the current document.
The WebDriver does not allow setting cookies for domains different from the domain of the current browsing context's document's domain.
This error also occurs when the document is not loaded via http://, https:// or ftp://, that is, cookie-averse document.
This error can occur when visiting a cookie-averse document, for example, a file on your local file system:
1from selenium import webdriver 2from selenium.common import exceptions 3 4session = webdriver.Firefox() 5session.get("file:///home/jdoe/document.html") 6try: 7 foo_cookie = {"name": "foo", "value": "bar"} 8 session.add_cookie(foo_cookie) 9except exceptions.InvalidCookieDomainException as e: 10 print(e.message)
Output:
InvalidCookieDomainException: Document is cookie-averse
It is not possible to add a cookie for the domain example.org if the current domain is example.com:
1from selenium import webdriver 2from selenium.common import exceptions 3 4session = webdriver.Firefox() 5session.get("https://example.com/") 6try: 7 cookie = {"name": "foo", 8 "value": "bar", 9 "domain": "example.org"} 10 session.add_cookie(cookie) 11except exceptions.InvalidCookieDomainException as e: 12 print(e.message)
Here are code snippets that can help you understand more how developers are using
Source:ErrorCodes.java  
...14package org.openqa.selenium.remote;15import org.openqa.selenium.ElementNotVisibleException;16import org.openqa.selenium.ImeActivationFailedException;17import org.openqa.selenium.ImeNotAvailableException;18import org.openqa.selenium.InvalidCookieDomainException;19import org.openqa.selenium.InvalidElementStateException;20import org.openqa.selenium.InvalidSelectorException;21import org.openqa.selenium.NoAlertPresentException;22import org.openqa.selenium.NoSuchElementException;23import org.openqa.selenium.NoSuchFrameException;24import org.openqa.selenium.NoSuchWindowException;25import org.openqa.selenium.SessionNotCreatedException;26import org.openqa.selenium.StaleElementReferenceException;27import org.openqa.selenium.TimeoutException;28import org.openqa.selenium.UnableToSetCookieException;29import org.openqa.selenium.UnhandledAlertException;30import org.openqa.selenium.UnsupportedCommandException;31import org.openqa.selenium.WebDriverException;32import org.openqa.selenium.XPathLookupException;33import org.openqa.selenium.interactions.InvalidCoordinatesException;34import org.openqa.selenium.interactions.MoveTargetOutOfBoundsException;35/**36 * Defines common error codes for the wire protocol.37 * 38 * @author jmleyba@gmail.com (Jason Leyba)39 */40public class ErrorCodes {41  // These codes were all pulled from ChromeCommandExecutor and seem all over the place.42  // TODO(jmleyba): Clean up error codes?43  public static final int SUCCESS = 0;44  public static final int NO_SUCH_SESSION = 6;45  public static final int NO_SUCH_ELEMENT = 7;46  public static final int NO_SUCH_FRAME = 8;47  public static final int UNKNOWN_COMMAND = 9;48  public static final int STALE_ELEMENT_REFERENCE = 10;49  public static final int ELEMENT_NOT_VISIBLE = 11;50  public static final int INVALID_ELEMENT_STATE = 12;51  public static final int UNHANDLED_ERROR = 13;52  public static final int ELEMENT_NOT_SELECTABLE = 15;53  public static final int JAVASCRIPT_ERROR = 17;54  public static final int XPATH_LOOKUP_ERROR = 19;55  public static final int TIMEOUT = 21;56  public static final int NO_SUCH_WINDOW = 23;57  public static final int INVALID_COOKIE_DOMAIN = 24;58  public static final int UNABLE_TO_SET_COOKIE = 25;59  public static final int UNEXPECTED_ALERT_PRESENT = 26;60  public static final int NO_ALERT_PRESENT = 27;61  public static final int ASYNC_SCRIPT_TIMEOUT = 28;62  public static final int INVALID_ELEMENT_COORDINATES = 29;63  public static final int IME_NOT_AVAILABLE = 30;64  public static final int IME_ENGINE_ACTIVATION_FAILED = 31;65  public static final int INVALID_SELECTOR_ERROR = 32;66  public static final int SESSION_NOT_CREATED = 33;67  public static final int MOVE_TARGET_OUT_OF_BOUNDS = 34;68  public static final int SQL_DATABASE_ERROR = 35;69  public static final int INVALID_XPATH_SELECTOR = 51;70  public static final int INVALID_XPATH_SELECTOR_RETURN_TYPER = 52;71  // The following error codes are derived straight from HTTP return codes.72  public static final int METHOD_NOT_ALLOWED = 405;73  /**74   * Returns the exception type that corresponds to the given {@code statusCode}. All unrecognized75   * status codes will be mapped to {@link WebDriverException WebDriverException.class}.76   * 77   * @param statusCode The status code to convert.78   * @return The exception type that corresponds to the provided status code or {@code null} if79   *         {@code statusCode == 0}.80   */81  public Class<? extends WebDriverException> getExceptionType(int statusCode) {82    switch (statusCode) {83      case SUCCESS:84        return null;85      case NO_SUCH_SESSION:86        return SessionNotFoundException.class;87      case INVALID_COOKIE_DOMAIN:88        return InvalidCookieDomainException.class;89      case UNABLE_TO_SET_COOKIE:90        return UnableToSetCookieException.class;91      case NO_SUCH_WINDOW:92        return NoSuchWindowException.class;93      case NO_SUCH_ELEMENT:94        return NoSuchElementException.class;95      case INVALID_SELECTOR_ERROR:96      case INVALID_XPATH_SELECTOR:97      case INVALID_XPATH_SELECTOR_RETURN_TYPER:98        return InvalidSelectorException.class;99      case MOVE_TARGET_OUT_OF_BOUNDS:100        return MoveTargetOutOfBoundsException.class;101      case NO_SUCH_FRAME:102        return NoSuchFrameException.class;103      case UNKNOWN_COMMAND:104      case METHOD_NOT_ALLOWED:105        return UnsupportedCommandException.class;106      case STALE_ELEMENT_REFERENCE:107        return StaleElementReferenceException.class;108      case ELEMENT_NOT_VISIBLE:109        return ElementNotVisibleException.class;110      case ELEMENT_NOT_SELECTABLE:111      case INVALID_ELEMENT_STATE:112        return InvalidElementStateException.class;113      case XPATH_LOOKUP_ERROR:114        return XPathLookupException.class;115      case ASYNC_SCRIPT_TIMEOUT:116      case TIMEOUT:117        return TimeoutException.class;118      case INVALID_ELEMENT_COORDINATES:119        return InvalidCoordinatesException.class;120      case IME_NOT_AVAILABLE:121        return ImeNotAvailableException.class;122      case IME_ENGINE_ACTIVATION_FAILED:123        return ImeActivationFailedException.class;124      case NO_ALERT_PRESENT:125        return NoAlertPresentException.class;126      case SESSION_NOT_CREATED:127        return SessionNotCreatedException.class;128      case UNEXPECTED_ALERT_PRESENT:129        return UnhandledAlertException.class;130      default:131        return WebDriverException.class;132    }133  }134  /**135   * Converts a thrown error into the corresponding status code.136   * 137   * @param thrown The thrown error.138   * @return The corresponding status code for the given thrown error.139   */140  public int toStatusCode(Throwable thrown) {141    if (thrown == null) {142      return SUCCESS;143    } else if (thrown instanceof TimeoutException) {144      return ASYNC_SCRIPT_TIMEOUT;145    } else if (thrown instanceof ElementNotVisibleException) {146      return ELEMENT_NOT_VISIBLE;147    } else if (thrown instanceof InvalidCookieDomainException) {148      return INVALID_COOKIE_DOMAIN;149    } else if (thrown instanceof InvalidCoordinatesException) {150      return INVALID_ELEMENT_COORDINATES;151    } else if (thrown instanceof InvalidElementStateException) {152      return INVALID_ELEMENT_STATE;153    } else if (thrown instanceof InvalidSelectorException) {154      return INVALID_SELECTOR_ERROR;155    } else if (thrown instanceof ImeNotAvailableException) {156      return IME_NOT_AVAILABLE;157    } else if (thrown instanceof ImeActivationFailedException) {158      return IME_ENGINE_ACTIVATION_FAILED;159    } else if (thrown instanceof NoAlertPresentException) {160      return NO_ALERT_PRESENT;161    } else if (thrown instanceof NoSuchElementException) {...Source:InvalidCookieDomainException.java  
...19 * Thrown when attempting to add a cookie under a different domain than the current URL.20 *21 * @see org.openqa.selenium.WebDriver.Options#addCookie(Cookie)22 */23public class InvalidCookieDomainException extends WebDriverException {24  public InvalidCookieDomainException() {25  }26  public InvalidCookieDomainException(String message) {27    super(message);28  }29  public InvalidCookieDomainException(Throwable cause) {30    super(cause);31  }32  public InvalidCookieDomainException(String message, Throwable cause) {33    super(message, cause);34  }35}...InvalidCookieDomainException
Using AI Code Generation
1import org.openqa.selenium.InvalidCookieDomainException;2import org.openqa.selenium.WebDriverException;3import org.openqa.selenium.WebDriver;4import org.openqa.selenium.WebDriverException;5import org.openqa.selenium.WebDriver;6import org.openqa.selenium.WebDriverException;7import org.openqa.selenium.WebDriver;8import org.openqa.selenium.WebDriverException;9import org.openqa.selenium.WebDriver;10import org.openqa.selenium.WebDriverException;11import org.openqa.selenium.WebDriver;12import org.openqa.selenium.WebDriverException;13import org.openqa.selenium.WebDriver;14import org.openqa.selenium.WebDriverException;15import org.openqa.selenium.WebDriver;16import org.openqa.selenium.WebDriverException;17import org.openqa.selenium.WebDriver;18import org.openqa.selenium.WebDriverException;19import org.openqa.selenium.WebDriver;20import org.openqa.selenium.WebDriverException;21import org.openqa.selenium.WebDriver;22import org.openqa.selenium.WebDriverException;23import org.openqa.selenium.WebDriver;24import org.openqa.selenium.WebDriverException;25import org.openqa.selenium.WebDriver;26import org.openqa.selenium.WebDriverException;27importInvalidCookieDomainException
Using AI Code Generation
1import org.openqa.selenium.InvalidCookieDomainException;2import org.openqa.selenium.WebDriver;3import org.openqa.selenium.chrome.ChromeDriver;4import org.openqa.selenium.chrome.ChromeOptions;5import org.openqa.selenium.Cookie;6import java.util.HashMap;7import java.util.Map;8public class Cookies {9    public static void main(String[] args) {10        System.setProperty("webdriver.chrome.driver", "C:\\\\Users\\\\sudhanshu\\\\Downloads\\\\chromedriver_win32\\\\chromedriver.exe");11        WebDriver driver = new ChromeDriver();12        Cookie cookie = new Cookie("test","cookie");13        driver.manage().addCookie(cookie);14        driver.manage().deleteCookie(cookie);15        driver.manage().deleteCookieNamed("test");16        driver.manage().deleteAllCookies();17        driver.manage().getCookies();18    }19}InvalidCookieDomainException
Using AI Code Generation
1import org.openqa.selenium.InvalidCookieDomainException;2import org.openqa.selenium.WebDriver;3import org.openqa.selenium.chrome.ChromeDriver;4import org.openqa.selenium.chrome.ChromeOptions;5import org.openqa.selenium.Cookie;6import org.openqa.selenium.By;7import org.openqa.selenium.JavascriptExecutor;8import java.util.Set;9import java.util.Iterator;10public class CookieException {11    public static void main(String[] args) {12        System.setProperty("webdriver.chrome.driver", "C:/chromedriver/chromedriver.exe");13        WebDriver driver = new ChromeDriver();14        Cookie cookie = new Cookie("cookieName", "cookieValue", "cookieDomain", "cookiePath", null);15        driver.manage().addCookie(cookie);16    }17}InvalidCookieDomainException
Using AI Code Generation
1package org.openqa.selenium;2public class InvalidCookieDomainException extends WebDriverException {3  public InvalidCookieDomainException(String message) {4    super(message);5  }6}7package org.openqa.selenium;8public class InvalidCookieDomainException extends WebDriverException {9  public InvalidCookieDomainException(String message) {10    super(message);11  }12}13package org.openqa.selenium;14public class InvalidCookieDomainException extends WebDriverException {15  public InvalidCookieDomainException(String message) {16    super(message);17  }18}19package org.openqa.selenium;20public class InvalidCookieDomainException extends WebDriverException {21  public InvalidCookieDomainException(String message) {22    super(message);23  }24}25package org.openqa.selenium;26public class InvalidCookieDomainException extends WebDriverException {27  public InvalidCookieDomainException(String message) {28    super(message);29  }30}31package org.openqa.selenium;32public class InvalidCookieDomainException extends WebDriverException {33  public InvalidCookieDomainException(String message) {34    super(message);35  }36}37package org.openqa.selenium;38public class InvalidCookieDomainException extends WebDriverException {39  public InvalidCookieDomainException(String message) {40    super(message);41  }42}43package org.openqa.selenium;44public class InvalidCookieDomainException extends WebDriverException {45  public InvalidCookieDomainException(String message) {46    super(message);47  }48}49package org.openqa.selenium;50public class InvalidCookieDomainException extends WebDriverException {51  public InvalidCookieDomainException(String message) {52    super(message);53  }54}55package org.openqa.selenium;56public class InvalidCookieDomainException extends WebDriverException {57  public InvalidCookieDomainException(String message) {58    super(message);59  }60}61package org.openqa.selenium;62public class InvalidCookieDomainException extends WebDriverException {63  public InvalidCookieDomainException(String message) {64    super(messageInvalidCookieDomainException
Using AI Code Generation
1package org.openqa.selenium;2public class InvalidCookieDomainException extends InvalidCookieException {3  public InvalidCookieDomainException(String message) {4    super(message);5  }6}7package org.openqa.selenium;8public class InvalidCookieException extends RuntimeException {9  public InvalidCookieException(String message) {10    super(message);11  }12}13package org.openqa.selenium;14public class InvalidCookiePathException extends InvalidCookieException {15  public InvalidCookiePathException(String message) {16    super(message);17  }18}19package org.openqa.selenium;20public class NoSuchCookieException extends WebDriverException {21  public NoSuchCookieException(String message) {22    super(message);23  }24}25package org.openqa.selenium;26public class WebDriverException extends RuntimeException {27  public WebDriverException() {28    super();29  }30  public WebDriverException(String message) {31    super(message);32  }33  public WebDriverException(String message, Throwable cause) {34    super(message, cause);35  }36  public WebDriverException(Throwable cause) {37    super(cause);38  }39}40package org.openqa.selenium;41public class WebDriverInfoException extends WebDriverException {42  public WebDriverInfoException(String message) {43    super(message);44  }45}46package org.openqa.selenium;47public class WebDriverTimeoutException extends WebDriverException {48  public WebDriverTimeoutException(String message) {49    super(message);50  }51}52package org.openqa.selenium;53public class WebDriverVersionException extends WebDriverException {54  public WebDriverVersionException(String message) {55    super(message);56  }57}58package org.openqa.selenium;59public class Cookie {60  private final String name;61  private final String value;62  private final String domain;63  private final String path;64  private final Date expiry;65  private final boolean secure;66  private final boolean httpOnly;67  public Cookie(String name, String value) {68    this(name, value, null, null, null, false, false);69  }70  public Cookie(String name, String value, String domain, String path, Date expiry, boolean secure, boolean httpOnly) {71    this.name = name;72    this.value = value;73    this.domain = domain;74    this.path = path;75    this.expiry = expiry;76    this.secure = secure;77    this.httpOnly = httpOnly;78  }79  public String getName() {80    return name;81  }82  public String getValue() {83    return value;84  }85  public String getDomain() {86    return domain;87  }88  public String getPath() {89    return path;90  }91  public Date getExpiry() {LambdaTest’s Selenium 4 tutorial is covering every aspects of Selenium 4 testing with examples and best practices. Here you will learn basics, such as how to upgrade from Selenium 3 to Selenium 4, to some advanced concepts, such as Relative locators and Selenium Grid 4 for Distributed testing. Also will learn new features of Selenium 4, such as capturing screenshots of specific elements, opening a new tab or window on the browser, and new protocol adoptions.
Upgrading From Selenium 3 To Selenium 4?: In this chapter, learn in detail how to update Selenium 3 to Selenium 4 for Java binding. Also, learn how to upgrade while using different build tools such as Maven or Gradle and get comprehensive guidance for upgrading Selenium.
What’s New In Selenium 4 & What’s Being Deprecated? : Get all information about new implementations in Selenium 4, such as W3S protocol adaption, Optimized Selenium Grid, and Enhanced Selenium IDE. Also, learn what is deprecated for Selenium 4, such as DesiredCapabilites and FindsBy methods, etc.
Selenium 4 With Python: Selenium supports all major languages, such as Python, C#, Ruby, and JavaScript. In this chapter, learn how to install Selenium 4 for Python and the features of Python in Selenium 4, such as Relative locators, Browser manipulation, and Chrom DevTool protocol.
Selenium 4 Is Now W3C Compliant: JSON Wireframe protocol is retiring from Selenium 4, and they are adopting W3C protocol to learn in detail about the advantages and impact of these changes.
How To Use Selenium 4 Relative Locator? : Selenium 4 came with new features such as Relative Locators that allow constructing locators with reference and easily located constructors nearby. Get to know its different use cases with examples.
Selenium Grid 4 Tutorial For Distributed Testing: Selenium Grid 4 allows you to perform tests over different browsers, OS, and device combinations. It also enables parallel execution browser testing, reads up on various features of Selenium Grid 4 and how to download it, and runs a test on Selenium Grid 4 with best practices.
Selenium Video Tutorials: Binge on video tutorials on Selenium by industry experts to get step-by-step direction from automating basic to complex test scenarios with Selenium.
LambdaTest also provides certification for Selenium testing to accelerate your career in Selenium automation testing.
Get 100 minutes of automation test minutes FREE!!
