我知道之前已经问过“以编程方式登录”的问题,但这是我想要做的。
我有许多python功能测试,每次登录Web应用程序以执行每个方案。登录已经成为最受测试的功能,我想跳过它。这意味着要避免UI节省时间。 我试图通过使用Requests python库发送一个帖子请求登录,然后以某种方式切换到webdriver并继续在授权部分进行测试。
这是我到目前为止所做的(代码来自另一个SO帖子)。
def login_robot(username,password):
import requests
# Fill in your details here to be posted to the login form.
payload = {'username': username, 'password': password}
login_url = "https://www.webapp.com/login/index.php"
# Use 'with' to ensure the session context is closed after use.
with requests.Session() as s:
s.post(login_url, data=payload)
# # An authorised request.
r = s.get('http://www.webapp.com/wall.php')
print r.text
r.text正确打印登录后面的页面。 我应该如何将它与启动实际firefox实例的webdriver连接? 这两个是完全不同的浏览器实例吗?
答案 0 :(得分:0)
可能不是最“程序化”的方法,但最简单的解决方案是制作一个基本可用于登录的基本“login_form.html”
<form id="login" action="https://www.webapp.com/login/index.php" method="post">
<input type="text" name="username" value="wehappyfew" />
<input type="password" name="password" value="qwerty1234" />
</form>
然后就这样做:
driver.get( "file:///C:/location/of/login_form.html" )
driver.find_element_by_css_selector( "form#login" ).submit()