Search code examples
pythonroblox

Changing ROBLOSECURITY Cookie in Selenium


I am attempting to change the cookie of .ROBLOSECURITY on www.roblox.com. This is for an attempted robot that will cycle through accounts.

I get the error:

"selenium.common.exceptions.UnableToSetCookieException: Message: unable to set cookie
(Session info: chrome=86.0.4240.75)"

This is the code so far...

with open("CookieList.txt") as CookieFile:
    Cookies = CookieFile.readlines()
    for Cookie in Cookies:
        PATH = "C:\Program Files (x86)\chromedriver.exe"
        driver = webdriver.Chrome(PATH)
        driver.get("https://www.roblox.com/games/" + GameID + "/Name/")
        #input("Log into your account in the new browser. Type 'Done' when finished.\n")
        roblocookie = {
            'name': "ROBLOSECURITY",
            'value': Cookie,
        }
        driver.add_cookie(roblocookie)

I've tried making "ROBLOSECURITY", ".ROBLOSECURITY" and changing the domains to what it is on the website, but to no avail.


Solution

  • To use any of the cookie handling methods in WebDriver, we first need to import the Cookie class. To do that, we use:

    import org.openqa.selenium.Cookie;
    

    Retrieve all cookies

    // This method gets all the cookies
    public Set<Cookie> getAllCookies() {
        return driver.manage().getCookies();
    }
    

    Retrieve a named cookie

    // This method gets a specified cookie
    public Cookie getCookieNamed(String name) {
        return driver.manage().getCookieNamed(name);
    }
    

    Retrieve the value of a cookie

    // This method gets the value of a specified cookie
    public String getValueOfCookieNamed(String name) {
        return driver.manage().getCookieNamed(name).getValue();
    }
    

    Add a cookie

    // This method adds or creates a cookie
    public void addCookie(String name, String value, String domain, String path, Date expiry) {
        driver.manage().addCookie(
        new Cookie(name, value, domain, path, expiry));
    }
    

    Add a set of cookies

    // This method adds set of cookies for a domain
    public void addCookiesToBrowser(Set<Cookie> cookies, String domain) {
        for (Cookie c : cookies) {
            if (c != null) {
                if (c.getDomain().contains(domain)){
                    driver.manage().addCookie(
                    new Cookie(name, value, domain, path, expiry));
                }
            }
        }
        driver.navigate().refresh();
    }
    

    Part of this answer was taken from here.