It raise an error message said "Can only set Cookies for the current domain",but all I did is just put the old cookies in.Sometime I add the 'correct' domain,it will raise error Message "Unable to set Cookie". And I tested it in Firefox,Firefox also cant work.
from selenium import webdriver
driver = webdriver.PhantomJS(executable_path=phantompath)
driver.get('http://stackoverflow.com/')
driver.get_screenshot_as_file('1.png')
cookies = driver.get_cookies()
driver.delete_all_cookies()
driver.get_cookies()
for cookie in cookies:
driver.add_cookie(cookie)
To use the PhantomJS webdriver, all you need to do is change it to PhantomJS(). Then will This will work with both Python 2.7 and Python 3. On Windows, the path should be changed to the location of your phantomjs installation.
Selenium considers PhantomJS as deprecated, so you need to us either Chrome or Firefox in headless mode.
The PhantomJS driver doesn't support all the keys from the cookie dictionary. One way to overcome this issue is to select the keys:
from selenium import webdriver
driver = webdriver.PhantomJS()
driver.get('http://stackoverflow.com/')
cookies = driver.get_cookies()
driver.delete_all_cookies()
for cookie in cookies :
driver.add_cookie({k: cookie[k] for k in ('name', 'value', 'domain', 'path', 'expiry')})
You need to change the domain parameter for each cookie. The domain field must be formatted like this:
driver = webdriver.PhantomJS()
driver.get('http://www.baidu.com')
driver.delete_all_cookies()
for item in cookie_dictionary:
driver.add_cookie({
'domain': '.baidu.com', # note the dot at the beginning
'name': item['name'],
'value': item['value'],
'path': '/',
'expires': None
})
driver.get('http://www.baidu.com')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With