トップページ > Selenium API(逆引き) > 【Java】getCookies

【Java】getCookies・・・全てのクッキーを取得する

【PR】業界トップクラスの「高単価報酬」「低マージン」を実現!レバテックフリーランス

「getCookies」でカレントドメインの全てのクッキーを取得出来ます。

取得したクッキーは、要素の重複を禁止する「Set」でリターンされます。

説明

◆メソッド
  ・java.util.Set<Cookie> getCookies()
◆使用形態
  ・driver.manage().getCookies()
◆備考
  ・カレントドメインの全てのクッキーを取得する
◆関連項目
  ・クッキー名からクッキーを取得する
  ・ダイアログの文字列を取得する

サンプル

import org.openqa.selenium.Cookie;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class WebTest{
  public static void main(String[] args){
    //Chrome制御のためChromeDriverのパスを指定
	System.setProperty("webdriver.chrome.driver",
		                            "D:\\AutoTest\\chromedriver.exe");
	//Chromeを起動する
	WebDriver driver = new ChromeDriver();
    //指定したURLに遷移する(※下記のアドレスは架空のアドレスです)
    driver.get("https://www.otameshi.co.jp");
    //現在のセッションでWebページが保持する全てのクッキーを表示
    System.out.println("---保持しているクッキー---");
    for(Cookie cookie : driver.manage().getCookies()){
        System.out.println(cookie);
    }
  }
}
※selenium version 4.1.4で動作確認をしています

実行結果

---保持しているクッキー---
ABC=A12; expires=金, 01 7 2018 01:23:01 JST; path=/;domain=www.otameshi.co.jp
DEF=D34; expires=土, 02 7 2016 01:33:12 JST; path=/;domain=www.otameshi.co.jp
GHI=G56; expires=日, 03 7 2017 01:43:22 JST; path=/;domain=www.otameshi.co.jp
JKL=J78; expires=月, 04 7 2016 01:53:55 JST; path=/;domain=www.otameshi.co.jp
※ここで設定されているクッキーの内容は架空の値です

Fatal error: Uncaught Google_Service_Exception: Error calling GET https://www.googleapis.com/analytics/v3/data/ga?ids=ga%3A138132118&start-date=7daysAgo&end-date=yesterday&metrics=ga%3Apageviews&dimensions=ga%3ApageTitle%2C+ga%3ApagePath&sort=-ga%3Apageviews&max-results=1000: (502) <!DOCTYPE html> <html lang=en> <meta charset=utf-8> <meta name=viewport content="initial-scale=1, minimum-scale=1, width=device-width"> <title>Error 502 (Server Error)!!1</title> <style> *{margin:0;padding:0}html,code{font:15px/22px arial,sans-serif}html{background:#fff;color:#222;padding:15px}body{margin:7% auto 0;max-width:390px;min-height:180px;padding:30px 0 15px}* > body{background:url(//www.google.com/images/errors/robot.png) 100% 5px no-repeat;padding-right:205px}p{margin:11px 0 22px;overflow:hidden}ins{color:#777;text-decoration:none}a img{border:0}@media screen and (max-width:772px){body{background:none;margin-top:0;max-width:none;padding-right:0}}#logo{background:url(//www.google.com/images/branding/googlelogo/1x/googlelog in /home/users/1/monda-muki/web/seleniumqref.com/google-api-php-client-1-master/src/Google/Http/REST.php on line 110