Selenium:尝试使用Cookie登录 - "只能为当前域设置Cookie"

sim*_*meg 12 python cookies post selenium http-post

我想要实现的目标

我正在尝试登录到必须使用Selenium无头启用cookie的网站,我使用PhantomJS作为驱动程序.

问题

我首先使用Selenium IDE记录了这个程序,它使用Firefox(不是无头)工作正常.然后我将代码导出到Python,现在我无法登录,因为它抛出错误"只能为当前域设置Cookie".我不知道为什么我会遇到这个问题,我不在正确的域名上吗?

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)

        ...
Run Code Online (Sandbox Code Playgroud)

我试过的

我已经尝试将cookie保存在dict中,转到另一个域,返回到原始域并添加cookie然后尝试登录但它仍然无效(如此线程中所示)

任何帮助表示赞赏.

Sai*_*fur 5

调查每个cookie对.我遇到了类似的问题,一些cookie属于谷歌.您需要确保cookie仅添加到当前域,并且也属于同一域.在这种情况下,您的例外是预期的.在旁注中,如果我没localhost记错,如果您这样做,则无法使用添加cookie.更改为IP地址.此外,调查您获得的特殊域名和到期信息.看,如果他们回来了null

编辑

我在Gmail上进行了这个简单的测试,以显示您做错了什么.初看起来我没注意到你正在尝试抓取部分cookie,一对,并将其添加到域中.因为,cookie没有任何域,路径,到期等信息,它试图将cookie添加到当前域(127.0.0.1)并抛出一些不太有意义的误导性信息.注意:为了成为有效的cookie,它必须具有您丢失的正确域和到期信息.

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()
Run Code Online (Sandbox Code Playgroud)

控制台输出:

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测试于上午9:59开始...

*******你正在做的部分cookie信息*******

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

完整Cookie包括域名和到期信息

{'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,'到期':*******,'域':'.google.com'}

注意:我只是*******故意替换了一些信息