我想在登录后获取会话变量和cookie。我已使用Selenium WebDriver并成功登录。但是如何在硒登录后如何获取会话和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();
        }


请尽快提供您的建议。

谢谢

最佳答案

我使用以下代码添加了代码,并在控制台中获得了以下值。

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

/ csologin /

progressr.login.uscourts.gov

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

空值



除了Thread.sleep(10000),您还可以尝试使用显式等待。我相信该网页花了一些时间来设置cookie,因为它正忙于等待页面加载。

希望这对您有帮助。

09-10 06:49
查看更多