###瀏覽器添加cookiespython
with open('cookies', 'r', encoding='utf-8') as f: list_cookies = json.loads(f.readline()) print(list_cookies) # cookie = [item["name"] + "=" + item["value"] for item in list_cookies] # cookiestr = '; '.join(item for item in cookie) # print(cookiestr) driver.get("https://jd.com") driver.delete_all_cookies() for cookie in list_cookies: driver.add_cookie(cookie) driver.get("https://order.jd.com/center/list.action") return driver
###抱錯chrome
selenium.common.exceptions.InvalidArgumentException: Message: invalid argument: invalid 'expiry' (Session info: chrome=77.0.3865.120)
###解決方法json
with open('cookies', 'r', encoding='utf-8') as f: list_cookies = json.loads(f.readline()) print(list_cookies) # cookie = [item["name"] + "=" + item["value"] for item in list_cookies] # cookiestr = '; '.join(item for item in cookie) # print(cookiestr) driver.get("https://jd.com") driver.delete_all_cookies() for cookie in list_cookies: if 'expiry' in cookie: del cookie['expiry'] driver.add_cookie(cookie) driver.get("https://order.jd.com/center/list.action") return driver