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

【Python】get_cookies・・・全てのクッキーを取得する

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

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

説明

◆メソッド
  ・get_cookies()
◆使用形態
  ・driver.get_cookies()
◆備考
  ・カレントドメインの全てのクッキーを取得する
◆関連項目
  ・ダイアログの文字列を取得する
  ・スクリーンショット(ファイル)を取得する

サンプル

from selenium import webdriver
from selenium.webdriver.chrome import service

#ChromeDriverのパスを変数に設定
CHROMEDRIVER = "D:\driver\chromedriver.exe"
#ChromeDriverのstartとstopを制御するServiceオブジェクトを介してパスを渡す
chrome_service = service.Service(executable_path=CHROMEDRIVER)
#Chromeを起動
driver = webdriver.Chrome(service=chrome_service)
#指定したURLに遷移(※下記のアドレスは架空のアドレスです)
driver.get("https://www.otameshi.co.jp")
#カレントドメインの全てのクッキーを取得する
allCookies = driver.get_cookies()
#取得したクッキーを表示
print(allCookies)
※selenium version 4.1.3で動作確認をしています

実行結果

[{'domain': '.otameshi.co.jp', 'expiry': 1548231234.314405, 'httpOnly': False, 'name': 'ABC', 'path': '/', 'secure': False, 'value': '3o8afdertygght3bsac2'}, {'domain': 'www.otameshi.co.jp', 'expiry': 19876576543, 'httpOnly': False, 'name': 'ffrtgy.3HgtYUJK.ddfGThy', 'path': '/', 'secure': False, 'value': 'joisjfYU'}]
※ここで設定されているクッキーの内容は架空の値です

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: (403) User does not have sufficient permissions for this profile. in /home/users/1/monda-muki/web/seleniumqref.com/google-api-php-client-1-master/src/Google/Http/REST.php:110 Stack trace: #0 /home/users/1/monda-muki/web/seleniumqref.com/google-api-php-client-1-master/src/Google/Http/REST.php(62): Google_Http_REST::decodeHttpResponse(Object(Google_Http_Request), Object(Google_Client)) #1 /home/users/1/monda-muki/web/seleniumqref.com/google-api-php-client-1-master/src/Google/Task/Runner.php(174): Google_Http_REST::doExecute(Object(Google_Client), Object(Google_Http_Request)) #2 /home/users/1/monda-muki/web/seleniumqref.com/google-api-php-client-1-master/src/Google/Http/REST.php(46): Google_Task_Runner->run() #3 /home/users/1/monda-muki/web/seleniumqref in /home/users/1/monda-muki/web/seleniumqref.com/google-api-php-client-1-master/src/Google/Http/REST.php on line 110