/ 我使用以下代码来获取cookie值,但我只获得第1和第2部分。但没有获得第3和第4部分(如您所见,为null)。 请帮帮我。我附上了手动获取的cookies的截图 /
{
"foo": 1,
"bar": 2,
"foo-bar": 3
}
输出: - ASPSESSIONIDSZQPRQCS; NFPFIAGDBMJNOMKPCPKESHDC; NULL; /; NULL;真
答案 0 :(得分:1)
您只能获得名称和价值。您不得获得其他域/路径或有效期的cookie。这是网络浏览器的安全政策。
答案 1 :(得分:0)
下面的代码可用于获取cookie值 包实用程序;
import java.util.Set;
import org.openqa.selenium.By;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.Cookie;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class CookieUtility {
public static InternetExplorerDriver getDriver() throws InterruptedException {
InternetExplorerDriver driver;
System.setProperty("webdriver.ie.driver",
"C:\\Documents\\Selenium\\IEDriverServer\\IEDriverServer.exe");
driver = new InternetExplorerDriver();
driver.get("https://example.com");
// Input Email id and Password If you are already Register
driver.findElement(By.name("username")).sendKeys("password");
driver.findElement(By.name("password")).sendKeys("Paswrd");
WebDriverWait wait = new WebDriverWait(driver, 5);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.id("logonButton")));
// WebElement ele = driver.findElement(By.id("ctllogonBtnButton__"));
element.sendKeys(Keys.ENTER);
for (int i = 0; i < 2 && driver.findElements(By.id("textbox")).size() == 0; i++) {
Thread.sleep(10000);
}
element.sendKeys(Keys.F5);
return driver;
}
public static String[] getCookieValues(InternetExplorerDriver driver) throws InterruptedException {
// create file named Cookies to store Login Information
Set<Cookie> cks = driver.manage().getCookies();
String[] cookieValues = new String[cks.size()];
int i = 0;
for (Cookie ck : cks) {
cookieValues[i] = ck.getValue();
i++;
}
i = 0;
return cookieValues;
}
public static String getSessionId(InternetExplorerDriver driver) {
String sessionId = driver.getSessionId().toString();
return sessionId;
}
public static void main(String args[]) throws InterruptedException {
InternetExplorerDriver driver = getDriver();
String[] values = getCookieValues(driver);
String sessionId = getSessionId(driver);
}
public static String getcookiestring(String sessionId, String cookie1, String cookie2, String cookie3) {
String cookie = "JSESSIONID=" + sessionId + "; hi.session.co.entity=" + cookie2 + "; hi.session.id.identifier="
+ cookie1 + "; hi.session.client.identifier=" + cookie3;
return cookie;
}
}