Selenium 2 get all cookies on domain

一个人想着一个人 提交于 2019-12-13 02:43:55

问题


All. I have problem with getting cookies on domain. i try get cookies:

def "go to site"() {
    when:
        go "http://bla-bla-bla.bla"
    then:
        title == "Bla-bla-bla"
        // check cookies
        String cookies = driver.manage().getCookieNamed("name1").getValue()
        println cookies
}

but cookies with name1 geting on other domain, not http://bla-bla-bla.bla, name1 it's cookies on domain http://ululu.ulu and a try get all cookies, on all domains(sites), but I did not get.

Please help me get all cookies on all domains(sites). Thank you. My English sucks.


回答1:


Selenium only gives you access to the cookies for the currently active domain. That is, cookies that are relevant to the current browser state.

There's no way around this that I know of.




回答2:


There is a workaround for this using a Mozilla firefox add-on which will save all cookies in XML format under current profile directory. This add-on will save cookies from all domains and can be accessed using webdriver.

For more details on implementation, refer to following blog: http://automationoverflow.blogspot.in/2013/07/getting-cookies-from-all-domains.html

Please remember to vote if this answer is helpful to you.




回答3:


You don´t need to use plugins to get all cookies (including httpOnly and secure cookies). If you use ChromeDriver, you can get all cookies from the browsers profile folder.

They are stored in an sqlite database file in ./profile/Default/Cookies

Example for java/selenium:

//set Browsers profile folder with ChromeOptions:

String intendedProfileDestinationPath = "C:/temp/somefolder";
ChromeOptions options = new ChromeOptions();
options.addArguments("user-data-dir="+intendedProfileDestinationPath);
WebDriver driver = new ChromeDriver(options);

//...visit one or more pages...    

//use sqlite to access file:

try {
  // db parameters
  String url = "jdbc:sqlite:"+pathToSqliteCookiesFile;
  // create a connection to the database
  conn = DriverManager.getConnection(url);
} catch (SQLException e) {
  System.out.println(e.getMessage());
}
String sql = "SELECT * FROM cookies";
ResultSet result = conn.createStatement().executeQuery(sql);
//... iterate resultset ...

Get sqlite drivers via maven:

<!-- https://mvnrepository.com/artifact/org.xerial/sqlite-jdbc -->
<dependency>
    <groupId>org.xerial</groupId>
    <artifactId>sqlite-jdbc</artifactId>
    <version>3.28.0</version>
</dependency>

If you only want cookies from the pages you visited, make sure to delete the complete folder contents if you restart your selenium browser.



来源:https://stackoverflow.com/questions/14872500/selenium-2-get-all-cookies-on-domain

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!