java 从硒会话中获取 cookie
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27501492/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get cookies from selenium session
提问by Atul Thakre
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:
我想在登录后获取会话变量和 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();
}
please provide your suggestion asap.
请尽快提供您的建议。
Thanks
谢谢
回答by Sighil
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
标题是PACER登录
Size: 1
尺寸:1
JSESSIONID
会话ID
/csologin/
/登录/
pacer.login.uscourts.gov
pacer.login.uscourts.gov
E44C8*********************400602
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.
除了 Thread.sleep(10000) 您还可以尝试使用显式等待。我相信该网页需要一些时间来设置 cookie,因为它正忙于等待页面加载。
Hope this helps you.
希望这对你有帮助。