1

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

Ajinkya
  • 22,324
  • 33
  • 110
  • 161
Atul Thakre
  • 502
  • 3
  • 8
  • 24
  • 1
    @leo: yes i have tried same driver.manage().getCookies(). but it returned empty []. – Atul Thakre Dec 16 '14 at 09:47
  • You chances of getting an answer here will increase dramatically if you show what you already tried. Are you sure there are visible cookies? When I log in to that site, I can't see any. – leo Dec 16 '14 at 09:55
  • possible duplicate of [Selenium WebDriver manager().getCookies() returns 0 always](http://stackoverflow.com/questions/15952262/selenium-webdriver-manager-getcookies-returns-0-always) – leo Dec 16 '14 at 09:56
  • 1
    @leo: i have tried Set allCookies = driver.manage().; for ( Cookie loadedCookie : allCookies) { System.out.println(String.format("%s -> %s", loadedCookie.getName(), loadedCookie.getValue())); } but it will returned empty[]; – Atul Thakre Dec 16 '14 at 09:59
  • @LEO: If you look at http live header after login.it will display session variable and cookies.here is sample: Set-Cookie: PacerSession=; Domain=; Path=/; Secure Set-Cookie: NextGenCSO=; Domain=.uscourts.gov; Path=/; Secure Set-Cookie: PacerClientCode=""; Domain=.uscourts.gov; Path=/; Secure Set-Cookie: PacerPref="receipt=Y"; Version=1; Domain=.uscourts.gov; Path=/; Secure Set-Cookie: ClientValidation=""; Domain=.uscourts.gov; Path=/; Secure Set-Cookie: ClientCodeDescription=""; Domain=.uscourts.gov; Path=/; Secure – Atul Thakre Dec 16 '14 at 10:02
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/66980/discussion-between-atu-tha-and-leo). – Atul Thakre Dec 16 '14 at 10:04
  • can u provide a valid username n password. With the code u gave i tried and got output as (although i wasn't able to login): `the title isPACER Login JSESSIONID -> 0BBD8CBB51C1156274D8FBBC5963xcde` – Vivek Singh Dec 16 '14 at 10:04

1 Answers1

0

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.

StrikerVillain
  • 3,719
  • 2
  • 24
  • 41