Selenium: Trying to log in with cookies - “Can only set cookies for current domain”

0
0

What I am trying to achieve

I am trying to log in to a website where cookies must be enabled using Selenium headless, I am using PhantomJS for driver.

Problem

I first recorded the procedure using Selenium IDE where it works fine using Firefox (not headless). Then I exported the code to Python and now I can’t log in because it’s throwing an error saying “Can only set Cookies for the current domain”. I don’t know why I am getting this problem, am I not on the correct domain?

Code

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
import unittest, time, re
self
.driver = webdriver.PhantomJS()
self
.driver.implicitly_wait(30)
self
.base_url = "https://login.example.com"
driver
= self.driver
driver
.get(self.base_url)
all_cookies
= self.driver.get_cookies()
# It prints out all cookies and values just fine
for cookie in all_cookies
print cookie['name'] + " --> " + cookies['value']
# Set cookies to driver
for s_cookie in all_cookies:
c
= { s_cookie['name'] : s_cookie['value']}
# This is where it's throwing an error saying "Can only set Cookies for current domain
driver
.add_cookie(c)
...

What I’ve tried

I’ve tried saving the cookies in a dict, going to another domain, going back to original domain and added the cookies and then trying to log in but it still doesn’t work (as suggested in this thread)

Any help is appreciated.

  • You must to post comments
0
0

Investigate the each cookies pairs. I ran into the similar issues and some of the cookies belonged to Google. You need to make sure cookies are being added only to the current Domain and also belong to the same Domain. In that case your exception is expected. On a side note, if I recall it correctly you cannot use localhost to add the cookies if you are doing so. Change to IP address. Also, investigate the cookies you are getting specially domain and expiry information. See, if they are returning null

Edit

I did this simple test on Gmail to show what you have done wrong. At first look I did not notice that you are trying to grab partial cookie, a pair, and add that to the domain. Since, the cookie does not have any Domain, path, expiry etc. information it was trying to add the cookie to current domain(127.0.0.1) and throwing some misleading info that did not quite make sense. Notice: in order to be a valid cookie it must have to have the correct Domain and expiry information which you have been missing.

import unittest
from selenium.webdriver.common.by import By
from selenium import webdriver
__author__
= 'Saifur'
class CookieManagerTest(unittest.TestCase):
def setUp(self):
self
.driver = webdriver.PhantomJS("E:\\working\\selenium.python\\selenium\\resources\\phantomjs.exe")
self
.driver.get("https://accounts.google.com/ServiceLogin?service=mail&continue=https://mail.google.com/mail/")
self
.driver.find_element(By.ID, "Email").send_keys("userid")
self
.driver.find_element(By.ID, "next").click()
self
.driver.find_element(By.ID, "Passwd").send_keys("supersimplepassword")
self
.driver.find_element(By.CSS_SELECTOR, "[type='submit'][value='Sign in']").click()
self
.driver.maximize_window()
def test(self):
driver
= self.driver
listcookies
= driver.get_cookies()
for s_cookie in listcookies:
# this is what you are doing
c
= {s_cookie['name']: s_cookie['value']}
print("*****The partial cookie info you are doing*****\n")
print(c)
# Should be done
print("The Full Cookie including domain and expiry info\n")
print(s_cookie)
# driver.add_cookie(s_cookie)
def tearDown(self):
self
.driver.quit()

Console output:

D:\Python34\python.exe “D:\Program Files (x86)\JetBrains\PyCharm Educational Edition 1.0.1\helpers\pycharm\utrunner.py” E:\working\selenium.python\selenium\python\FirstTest.py::CookieManagerTest true
Testing started at 9:59 AM …

*******The partial cookie info you are doing*******

{‘PREF’: ‘ID=*******:FF=0:LD=en:TM=*******:LM=*******:GM=1:S=*******’}

The Full Cookie including domain and expiry info

{‘httponly’: False, ‘name’: ‘*******’, ‘value’: ‘ID=*******:FF=0:LD=en:TM=*******:LM=1432393656:GM=1:S=iNakWMI5h_2cqIYi’, ‘path’: ‘/’, ‘expires’: ‘Mon, 22 May 2017 15:07:36 GMT’, ‘secure’: False, ‘expiry’: *******, ‘domain’: ‘.google.com’}

Notice: I just replaced some info with ******* on purpose

  • You must to post comments
Showing 1 result
Your Answer
Post as a guest by filling out the fields below or if you already have an account.
Name*
E-mail*
Website