【问题标题】:Changing ROBLOSECURITY Cookie in Selenium在 Selenium 中更改 ROBLSECURITY Cookie
【发布时间】:2021-02-01 06:25:17
【问题描述】:

我正在尝试更改 www.roblox.com 上的 .ROBLSECURITY 的 cookie。这是针对将循环浏览帐户的尝试过的机器人。

我得到错误:

“selenium.common.exceptions.UnableToSetCookieException:消息:无法设置 cookie
(会话信息:chrome=86.0.4240.75)"

这是目前为止的代码...

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)

我尝试制作“ROBLOSECURITY”、“.ROBLOSECURITY”并将域更改为网站上的内容,但无济于事。

【问题讨论】:

    标签: python roblox


    【解决方案1】:

    要使用 WebDriver 中的任何 cookie 处理方法,我们首先需要导入 Cookie 类。为此,我们使用:

    import org.openqa.selenium.Cookie;
    

    检索所有 cookie

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

    检索命名的 cookie

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

    检索 cookie 的值

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

    添加 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));
    }
    

    添加一组 cookie

    // 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();
    }
    

    部分答案来自here。

    【讨论】:

    • 什么语言?这个问题用 Python 标记,但代码示例没有。是 Java 吗?
    【解决方案2】:

    没有办法编辑 cookie。您必须删除 cookie,然后创建一个新的。

    删除一个 cookie:

    from selenium import webdriver
    driver = webdriver.Chrome()
    
    # Navigate to the URL
    driver.get("http://www.example.com")
    driver.add_cookie({"name": "test1", "value": "cookie1"})
    driver.add_cookie({"name": "test2", "value": "cookie2"})
    
    # Delete a cookie with name 'test1'
    driver.delete_cookie("test1")
    

    创建一个 cookie:

    from selenium import webdriver
    
    driver = webdriver.Chrome()
    
    driver.get("http://www.example.com")
    
    # Adds the cookie into current browser context
    driver.add_cookie({"name": "key", "value": "value"})
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-11-01
      • 2014-03-31
      • 2011-06-22
      • 1970-01-01
      • 2021-10-12
      • 2018-08-28
      • 1970-01-01
      相关资源
      最近更新 更多