gpt4 book ai didi

java - 从 selenium session 中获取 cookie

转载 作者:太空宇宙 更新时间:2023-11-04 14:17:57 24 4
gpt4 key购买 nike

我想在登录后获取 session 变量和cookie。我已经使用selenium webdriver并成功登录。但是如何在selenium中登录后获取 session 和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

JSSessionID

/csologin/

pacer.login.uscourts.gov

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

<小时/>

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

希望这对您有帮助。

关于java - 从 selenium session 中获取 cookie,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27501492/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com