0

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.

tim_yates
  • 167,322
  • 27
  • 342
  • 338
plsgogame
  • 1,334
  • 15
  • 28
  • Do you mean 1) you're seeing cookies from all domains and you ONLY want cookies from the bla-bla-bla domain OR 2) You are only seeing cookies from the bla-bla-bla domain and you want cookies from all domains? – tim_yates Feb 14 '13 at 10:38
  • 2 option, you can help me? – plsgogame Feb 14 '13 at 10:54
  • check out answer to a similar question [here][1] [1]: http://stackoverflow.com/a/22238612/296280 – Max Zalota Mar 07 '14 at 02:21

3 Answers3

3

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.

Luke Daley
  • 571
  • 3
  • 6
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.

fire
  • 149
  • 1
  • 5
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.

Vishnu
  • 319
  • 2
  • 4