如何在selenium python中打印浏览器存储中显示的cookie过期日期?

如何在selenium python中打印浏览器存储中显示的cookie过期日期?,python,selenium,selenium-webdriver,Python,Selenium,Selenium Webdriver,我想打印cookie属性,如浏览器应用程序存储中显示的名称、值、域、过期日期等 我尝试了以下修补程序: def testcase_03(self,test_setup): self.driver.get("https://lifesciences.cactusglobal.com/") print("Page title: " + self.driver.title) wanted_cookie=self.driver.get_c

我想打印cookie属性,如浏览器应用程序存储中显示的名称、值、域、过期日期等

我尝试了以下修补程序:

def testcase_03(self,test_setup):
    self.driver.get("https://lifesciences.cactusglobal.com/")
    print("Page title: " + self.driver.title)

    wanted_cookie=self.driver.get_cookie('__ivc')
    print(wanted_cookie)
输出如下:

Page title: High-Impact Scientific Communication Solutions | Cactus Life Sciences
{'domain': '.cactusglobal.com', 'expiry': 1684985602, 'httpOnly': False, 'name': '__ivc', 'path': '/', 'secure': False, 'value': 'a99fe4ba-7ff5-400f-90f9-422313b1c3c3'}
现在,可以理解chrome应用程序存储中的到期时间
2023-05-25T12:16:44.000Z


是否仍要打印浏览器中显示的值?

我认为您已经得到响应,需要执行以下三项操作:

1)解析JSON响应。

//Convert response to proper JSON after replacing single quotes with double
wanted_cookie = wanted_cookie.replace("'", "\"")
//Load Json
wanted_cookie= json.loads(wanted_cookie)
expiry = wanted_cookie["expiry"]
print(expiry)
convertedDT = time.strftime('%Y-%m-%dT%H:%M:%S.000Z', time.localtime(expiry))
 print(convertedDT)
2)从JSON中提取到期键值。

//Convert response to proper JSON after replacing single quotes with double
wanted_cookie = wanted_cookie.replace("'", "\"")
//Load Json
wanted_cookie= json.loads(wanted_cookie)
expiry = wanted_cookie["expiry"]
print(expiry)
convertedDT = time.strftime('%Y-%m-%dT%H:%M:%S.000Z', time.localtime(expiry))
 print(convertedDT)
3)根据您的要求格式化日期和时间。

//Convert response to proper JSON after replacing single quotes with double
wanted_cookie = wanted_cookie.replace("'", "\"")
//Load Json
wanted_cookie= json.loads(wanted_cookie)
expiry = wanted_cookie["expiry"]
print(expiry)
convertedDT = time.strftime('%Y-%m-%dT%H:%M:%S.000Z', time.localtime(expiry))
 print(convertedDT)
完整代码:

def testcase_03(self,test_setup):
    self.driver.get("https://lifesciences.cactusglobal.com/")
    print("Page title: " + self.driver.title)
    wanted_cookie=self.driver.get_cookie('__ivc')
    print(wanted_cookie)
    wanted_cookie = wanted_cookie.replace("'", "\"")
    wanted_cookie= json.loads(wanted_cookie)
    expiry = wanted_cookie["expiry"]
    print(expiry)
    convertedDT = time.strftime('%Y-%m-%dT%H:%M:%S.000Z', time.localtime(expiry))
    print(convertedDT)
注意:请按照步骤3所示的要求格式化expiryKey。

//Convert response to proper JSON after replacing single quotes with double
wanted_cookie = wanted_cookie.replace("'", "\"")
//Load Json
wanted_cookie= json.loads(wanted_cookie)
expiry = wanted_cookie["expiry"]
print(expiry)
convertedDT = time.strftime('%Y-%m-%dT%H:%M:%S.000Z', time.localtime(expiry))
 print(convertedDT)