我在使用请求库为登录功能编写验收测试时遇到问题。登录是使用 post 方法实现的,我无法直接更改查询字符串和操作 url。我想要做的是手动提交帖子数据并将页面重定向到如果用户成功登录它会定期访问的页面。我想检索该页面的 html 并检查是否成功登录,如何我会这样做吗?
此方法失败:
data={'email': 'example@example.com', 'password': 'example'}
login_url="examplepage.com/signin"
request_data = requests.post(login_url, data, allow_redirects=True)
print request_data.content
这是 resp.headers 返回的:
{'content-length': '1124', 'date': 'Sun, 14 Apr 2013 16:12:51 GMT', 'set-cookie': 'session="+udnwfCkuAuFGp9QKOiU1YS2X1s=?_fresh=STAwCi4=&_id=Uyc3XHhkZVx4Y2J3T1x4YjBceDkxeFx4MWNceGFhXHhkNFx4OTMhKVx4MGJyJwpwMQou&csrf=Uyc2Mzk0YjVjZjMzMGZkMTNkY2NiM2EzZTZkYzgyMjhkY2IwOWQ2NmM3JwpwMQou"; Path=/; HttpOnly', 'content-type': 'text/html; charset=utf-8', 'connection': 'keep-alive', 'server': 'Werkzeug/0.8.3 Python/2.7.3'}
resp.content 只返回登录页面的 html:
<title>Web App</title>
</head>
<body>
<div>Web App: <a href="/home">Home</a>
| <a href="/signin">Login</a>
| <a href="/register">Register</a>
</div>
<hr>
<h1>Sign in</h1>
<form action="" method=post name="signin">
<div style="display:none;"><input id="csrf_token" name="csrf_token" type="hidden" value="20130414164251##fd7e82d47974518d098b41cecf2a4452f890317f"></div>
<dl>
<dt><label for="email">Email Address</label>
<dd><input id="email" name="email" type="text" value="example@example.com">
</dd>
<dt><label for="password">Password</label>
<dd><input id="password" name="password" type="password" value="">
</dd>
</dl>
<p><input type="submit" value="Sign in">
</form>
<a href="/login"> Click here to sign in using your Google, Yahoo, AOL, Flickr, or another OpenID account. </a>
</body>
</html>