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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何使用selenium从WebDriver获取cookie值?相关的知识,希望对你有一定的参考价值。

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

 WebDriver driver;
        System.setProperty("webdriver.ie.driver",
                "C:UsersMR049860DocumentsSeleniumIEDriverServerIEDriverServer.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

答案

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

另一答案

下面的代码可用于获取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:DocumentsSeleniumIEDriverServerIEDriverServer.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;

    }

}

以上是关于如何使用selenium从WebDriver获取cookie值?的主要内容,如果未能解决你的问题,请参考以下文章

如何使用C#中的Selenium Webdriver使用Chrome浏览器的Cookie?

如何使用 Selenium WebDriver 获取 Inspect Element 代码

在 Selenium WebDriver 上,如何从文本标签内的标题标签中获取文本

如何使用Selenium C#WebDriver查找所有父元素?

在 Selenium WebDriver 上如何从 Span Tag 中获取文本

如何使用 selenium webdriver C# 获取元素样式属性的值