【问题标题】:Python selenium rc create_cookiePython selenium rc create_cookie
【发布时间】:2013-10-05 14:37:53
【问题描述】:

我似乎无法弄清楚如何使用 python 为 Selenium 设置带有方括号 [ ] 的 cookie。这就是我正在尝试的:

selenium.create_cookie("test[country]=nl", "path=/, max_age=6000")

结果:

Traceback (most recent call last):
  File "test.py", line 55, in test
    sel.create_cookie('test[country]=nl', "path=/, max_age=6000")
  File "C:\Python27\lib\site-packages\selenium\selenium.py", line 1813, in create_cookie
    self.do_command("createCookie", [nameValuePair,optionsString,])
  File "C:\Python27\lib\site-packages\selenium\selenium.py", line 225, in do_command
    raise Exception(data)
Exception: ERROR: Invalid parameter.

我该如何解决这个问题?

编辑: 这是部分代码。它基于IDE导出的代码。

from selenium.selenium import selenium
import unittest, time, re
from selenium import webdriver

class country(unittest.TestCase):
    def setUp(self):
        self.verificationErrors = []
        self.selenium = selenium("localhost", 4444, "*chrome", "http://example.com/")
        self.selenium.start()

    def test_country_cookie_redirect(self):
        sel = self.selenium
        sel.create_cookie('test[country]=nl', "path=/, max_age=6000")
        sel.open("http://example.com")
        self.assertEqual("http://example.com/nl/nld", sel.get_location()) 

    def tearDown(self):
        self.selenium.stop()
        self.assertEqual([], self.verificationErrors)

if __name__ == "__main__":
    unittest.main()

【问题讨论】:

    标签: python selenium selenium-webdriver


    【解决方案1】:

    使用add_cookie()。例如:

    from selenium import webdriver
    
    browser = webdriver.Firefox()
    browser.get("http://stackoverflow.com")
    
    browser.add_cookie({"name": "test[country]",
                        "value": "nl",
                        "path": "/",
                        "max_age": 6000})
    
    browser.close()
    

    UPD(使用 webdriver 修改测试用例):

    import unittest
    from selenium import webdriver
    
    
    class country(unittest.TestCase):
        def setUp(self):
            self.verificationErrors = []
            self.driver = webdriver.Remote("http://127.0.0.1:4444/wd/hub", {'browserName': 'chrome'})
    
        def test_country_cookie_redirect(self):
            self.driver.add_cookie({"name": "test[country]",
                                    "value": "nl",
                                    "path": "/",
                                    "max_age": 6000})
            self.driver.get("http://example.com")
            self.assertEqual("http://example.com", self.driver.current_url)
    
        def tearDown(self):
            self.driver.quit()
            self.assertEqual([], self.verificationErrors)
    
    
    if __name__ == "__main__":
        unittest.main()
    

    【讨论】:

    • Webdriver 与 selenium 服务器 (RC) 不同,对吧?我需要它与 RC 一起使用。
    • @user2511309 好吧,当然。如果我错了,请纠正我:你有一个 selenium 服务器并且你正在使用 selenium remote webdriver?
    • 我是 Selenium 的新手,所以我想我把事情搞混了。我用 IDE 创建了一些脚本并将它们导出到 Python。它们都以 self.selenium = selenium("localhost", 4444, "*chrome", "example.com/").self.selenium.open("exmapele.com") 等开头。与 webdriver.get 有什么不同?要按照您的建议使用 add_cookie(),我应该将导出的其余部分重写为“webdriver”代码,对吗?
    • @user2511309 好的,首先提供您迄今为止编写的代码(更新问题)。谢谢。
    猜你喜欢
    • 2012-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-13
    • 2011-01-12
    • 2011-03-04
    • 2011-04-28
    • 2011-11-23
    相关资源
    最近更新 更多