Can only set Cookies for the current domain

时间:2024-01-08 17:06:15
 # -*- coding: utf-8 -*-
"""
Created on Mon Dec 12 14:35:49 2016 @author: yaru
""" targetURL = "http://pythonscraping.com"
#targetURL = "https://github.com/" from selenium import webdriver
driver = webdriver.PhantomJS(executable_path=r"D:\Anaconda2\Scripts\phantomjs-2.1.1-windows\bin\phantomjs.exe")
driver.get(targetURL)
driver.implicitly_wait(1)
#print(driver.get_cookies()) savedCookies = driver.get_cookies()
driver2 = webdriver.PhantomJS(executable_path=r"D:\Anaconda2\Scripts\phantomjs-2.1.1-windows\bin\phantomjs.exe")
driver2.get(targetURL)
driver2.implicitly_wait(1)
driver2.delete_all_cookies() for cookie in savedCookies:
# fix the 2nd problem
for k in ('name', 'value', 'domain', 'path', 'expiry'):
if k not in list(cookie.keys()):
if k == 'expiry':
cookie[k] = 1475825481
# fix the 1st problem
driver2.add_cookie({k: cookie[k] for k in ('name', 'value', 'domain', 'path', 'expiry') if k in cookie})
print(cookie)
print("aaaa3") driver2.get(targetURL)
driver2.implicitly_wait(1)