如何使用selenium从WebDriver获取cookie值?

问题描述 投票:0回答:2

/我使用以下代码来获取cookie值,但我只得到第1和第2部分。但没有得到第3和第4部分(null,你可以看到)。请帮帮我。我已经附上我手动获得的cookie的截图/ enter image description here

 WebDriver driver;
        System.setProperty("webdriver.ie.driver",
                "C:\\Users\\MR049860\\Documents\\Selenium\\IEDriverServer\\IEDriverServer.exe");
        driver = new InternetExplorerDriver();
        driver.get("https://www.example.com");

        // Input Email id and Password If you are already Register
        driver.findElement(By.name("j_username")).sendKeys("publisher");
        driver.findElement(By.name("j_password")).sendKeys("Passw0rd");
        WebDriverWait wait = new WebDriverWait(driver, 5);
        WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.id("BtnButton__")));
        // WebElement ele = driver.findElement(By.id("ctllogonBtnButton__"));
        element.sendKeys(Keys.ENTER);
        // create file named Cookies to store Login Information     
        File file = new File("madhu.data");                         
        try     
        {     
            // Delete old file if exists
            file.delete();      
            file.createNewFile();           
            FileWriter fileWrite = new FileWriter(file);                            
            BufferedWriter Bwrite = new BufferedWriter(fileWrite);                          
            // loop for getting the cookie information      

            // loop for getting the cookie information      
            for(Cookie ck : driver.manage().getCookies())                           
            {           
                Bwrite.write((ck.getName()+";"+ck.getValue()+";"+ck.getDomain()+";"+ck.getPath()+";"+ck.getExpiry()+";"+ck.isSecure()));                                                                                                    
                Bwrite.newLine();             
            }           
            Bwrite.close();         
            fileWrite.close();  

        }
        catch(Exception ex)                 
        {       
            ex.printStackTrace();           
        }

输出: - ASPSESSIONIDSZQPRQCS; NFPFIAGDBMJNOMKPCPKESHDC; null; /; null; true

java selenium cookies selenium-webdriver
2个回答
1
投票

你只能得到名字和价值。您不得获得其他域/路径或有效期的cookie。这是Web浏览器的安全策略。


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;

    }

}
© www.soinside.com 2019 - 2024. All rights reserved.