【问题标题】:Robot framework pass cookie to get request (RequestsLibrary) - TypeError机器人框架传递 cookie 以获取请求 (RequestsLibrary) - TypeError
【发布时间】:2017-09-18 12:12:33
【问题描述】:

我需要将 cookie 值传递给 Get 请求关键字以使其工作,但它仍然失败。我不确定如何在那里正确传递它。关键字的文档说明了标头:“标头:与请求一起使用的标头字典” 我有这样的测试,使用库RequestsLibrary:

Check request and response
    Do Local Login  ${username}    ${password}  #this is my custom keyword - opens browser and logs in using UI
    ${cookie_value}=  Get Cookie Value  JSESSIONID
    Create Session  session1  https://oururl.com  verify=${False}  cookies=${cookie_value}
    ${dict}=  Create Dictionary   Cookie=${cookie_value}
    ${resp}=  Get request  session1  /somepage/request  headers=${dict}

它总是返回TypeError: string indices must be integers。 追溯:

        Traceback (most recent call last):
  File "c:\python27\lib\site-packages\RequestsLibrary\RequestsKeywords.py", line 370, in get_request
    session, uri, params, headers, json, redir, timeout)
  File "c:\python27\lib\site-packages\RequestsLibrary\RequestsKeywords.py", line 897, in _get_request
    verify=self.verify)
  File "c:\python27\lib\site-packages\requests\sessions.py", line 501, in get
    return self.request('GET', url, **kwargs)
  File "c:\python27\lib\site-packages\requests\sessions.py", line 474, in request
    prep = self.prepare_request(req)
  File "c:\python27\lib\site-packages\requests\sessions.py", line 385, in prepare_request
    cookies = cookiejar_from_dict(cookies)
  File "c:\python27\lib\site-packages\requests\cookies.py", line 518, in cookiejar_from_dict
    cookiejar.set_cookie(create_cookie(name, cookie_dict[name]))

我确实需要将 cookie 传递给请求,否则它会再次返回仅登录页面。

非常感谢您的帮助!

【问题讨论】:

  • 最后一行是否会产生错误?
  • 是的,最后一个。我添加了回溯

标签: python cookies automated-tests python-requests robotframework


【解决方案1】:

Sooo终于找到了解决办法:

    Do Local Login  ${username}    ${password}
    ${cookie_value}=  Get Cookie Value  JSESSIONID
    ${dict}=  Create Dictionary   JSESSIONID=${cookie_value}
    Create Session  session1  https://oururl.com  verify=${False}  cookies=${dict}
    ${resp}=  Get request  session1  /somepage/request

关键字Create Session 与cookie 一起使用,只是cookie 不仅必须作为cookie 值传递,还必须作为字典JSESSIONID=${cookie_value} 传递。 或者,我不必在Create Session 中传递cookie,而是在Get Request 中传递cookie,然后我必须将cookie 设为像${dict}= Create Dictionary Cookie=JSESSIONID=${cookie_value} 这样的字典,然后传递给Get request session1 /somepage/request headers=${dict}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-06-09
    • 2018-07-25
    • 2020-12-07
    • 1970-01-01
    • 2014-10-31
    • 2023-04-01
    • 2021-09-18
    相关资源
    最近更新 更多