'Get cookies from selenium session

I want to get session variable and cookies after login. i have used selenium webdriver and successfully login. but how to get session and cookies after login in selenium.here is my code:

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

please provide your suggestion asap.

Thanks



Solution 1:[1]

I ran your code with the following code additions and was able to get the following value in my console.

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

console

the title isPACER Login

Size: 1

JSESSIONID

/csologin/

pacer.login.uscourts.gov

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

null


Instead of Thread.sleep(10000) you can also try using explicit wait. I believe that the web page took some time to set the cookies since it was busy waiting for the page to load.

Hope this helps you.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Sighil