【问题标题】:Get cookies from selenium session从 selenium 会话中获取 cookie
【发布时间】:2015-02-14 13:53:37
【问题描述】:

我想在登录后获取会话变量和 cookie。我已经使用 selenium webdriver 并成功登录。但是如何在 selenium 登录后获取会话和 cookie。这是我的代码:

try {
            WebDriver driver = new FirefoxDriver();
            driver.get("https://pacer.login.uscourts.gov/csologin/login.jsf");
            System.out.println("the title is"+driver.getTitle());
            WebElement id= driver.findElement(By.xpath("/html/body/div[3]/div/div[1]/div[1]/div[15]/div[2]/form/table[1]/tbody/tr/td[2]/input"));
            WebElement pass=driver.findElement(By.xpath("/html/body/div[3]/div/div[1]/div[1]/div[15]/div[2]/form/table[2]/tbody/tr/td[2]/input"));
            WebElement button=driver.findElement(By.xpath("/html/body/div[3]/div/div[1]/div[1]/div[15]/div[2]/form/div[2]/button[1]"));

            id.sendKeys("USERNAME");
            pass.sendKeys("PASSWORD");
            button.click();
        } catch (Exception e) {

            e.printStackTrace();
        }

请尽快提供您的建议。

谢谢

【问题讨论】:

  • @leo:是的,我尝试过相同的 driver.manage().getCookies()。但它返回空 []。
  • 如果你展示你已经尝试过的东西,你在这里得到答案的机会会大大增加。你确定有可见的cookies吗?当我登录该网站时,我看不到任何内容。
  • @leo: 我试过 Set allCookies = driver.manage().; for ( Cookie loadedCookie : allCookies) { System.out.println(String.format("%s -> %s", loadedCookie.getName(), loadedCookie.getValue())); } 但它会返回 empty[];
  • @LEO:如果您在登录后查看http live header。它将显示会话变量和cookies。这里是示例: Set-Cookie: PacerSession=;域=;路径=/; Secure Set-Cookie: NextGenCSO=;域名=.uscourts.gov;路径=/;安全设置 Cookie:PacerClientCode="";域名=.uscourts.gov;路径=/;安全设置 Cookie:PacerPref="receipt=Y";版本=1;域名=.uscourts.gov;路径=/;安全设置 Cookie:ClientValidation="";域名=.uscourts.gov;路径=/;安全设置 Cookie:ClientCodeDescription="";域名=.uscourts.gov;路径=/;安全

标签: java selenium selenium-webdriver webdriver


【解决方案1】:

我使用以下代码添加运行您的代码,并能够在我的控制台中获得以下值。

try {
        WebDriver driver = new FirefoxDriver();
        driver.get("https://pacer.login.uscourts.gov/csologin/login.jsf");
        System.out.println("the title is" + driver.getTitle());
        WebElement id = driver
                .findElement(By
                        .xpath("/html/body/div[3]/div/div[1]/div[1]/div[15]/div[2]/form/table[1]/tbody/tr/td[2]/input"));
        WebElement pass = driver
                .findElement(By
                        .xpath("/html/body/div[3]/div/div[1]/div[1]/div[15]/div[2]/form/table[2]/tbody/tr/td[2]/input"));
        WebElement button = driver
                .findElement(By
                        .xpath("/html/body/div[3]/div/div[1]/div[1]/div[15]/div[2]/form/div[2]/button[1]"));

        id.sendKeys("USERNAME");
        pass.sendKeys("PASSWORD");
        button.click();

        Thread.sleep(10000);
        Set<Cookie> cookies = driver.manage().getCookies();
        System.out.println("Size: " + cookies.size());

        Iterator<Cookie> itr = cookies.iterator();
        while (itr.hasNext()) {
            Cookie cookie = itr.next();
            System.out.println(cookie.getName() + "\n" + cookie.getPath()
                    + "\n" + cookie.getDomain() + "\n" + cookie.getValue()
                    + "\n" + cookie.getExpiry());
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

控制台

标题为PACER登录

尺寸:1

JSESSIONID

/cslogin/

pacer.login.uscourts.gov

E44C8*********************400602


您也可以尝试使用显式等待来代替 Thread.sleep(10000)。我认为该网页需要一些时间来设置 cookie,因为它正忙于等待页面加载。

希望对你有所帮助。

【讨论】:

    猜你喜欢
    • 2011-09-09
    • 1970-01-01
    • 1970-01-01
    • 2012-04-22
    • 2019-10-31
    • 2011-09-07
    • 2015-07-29
    • 2011-05-17
    • 2011-04-20
    相关资源
    最近更新 更多