首页 > 编程 > Python > 正文

Python 自动化表单提交实例代码

2020-02-16 01:40:48
字体:
来源:转载
供稿:网友

今天以一个表单的自动提交,来进一步学习selenium的用法

练习目标

  0)运用selenium启动firefox并载入指定页面(这部分可查看本人文章 http://www.cnblogs.com/liu2008hz/p/6958126.html)

  1)页面元素查找(多种查找方式:find_element_*)

  2)内容填充(send_keys)

  3)iframe与父页面切换(switch_to_frame是切换到iframe,switch_to_default_content是切换到主页面)

  4)浏览器交互处理:window.alert, window.confirm, window.prompt

    与上面的三个浏览器交互内容,需要用到switch_to_alert,有几个用法需要注意:

    a)accept():发送确定指令,相当于点击“确定”按钮

    b)dismiss():取消操作,相当于点击“取消”按钮或点击右上角“关闭”

    c)send_keys:填充prompt框需要填写的内容 

准备工作

  html页面(注册页,内嵌一个注册表单;之所以这样举例,是为了介绍练习selenium的switch_to_frame的用法)

  1)注册页面(路径D:/RegisterDEMO/index.htm)  

<!DOCTYPE><html><head> <title>用户注册</title> <meta charset="utf-8" /></head><body> <h3>测试Python selenium自动提交表单</h3> <iframe id="register_iframe" width="320" height="200" border="0" src="register.htm" /></body></html>

   2)注册表单(路径D:/RegisterDEMO/register.htm)

<!DOCTYPE><html><head> <title>这是内嵌表单</title> <meta charset="utf-8" /> <style type="text/css">  input[type='text']{border:1px solid #abc; font-size:14px; padding:5px; width:200px;}  input[type='password']{border:1px solid #abc; font-size:14px; padding:5px; width:200px;}  input[type='submit']{border:1px solid #abc; font-size:14px; padding:5px 10px; width:100px; cursor:pointer; margin-top:20px;}  input[type='submit']:hover{background-color:#aaaaff;} </style></head><body> <form action="/register/regaction" method="POST">  <table>   <tr>    <td>用户名:</td>    <td><input id="txt_account" type="text" value="" placeholder="用户名" /></td>   </tr>   <tr>    <td>密码:</td>    <td><input id="txt_password" type="password" value="" placeholder="密码" /></td>   </tr>   <tr>    <td>电子邮箱:</td>    <td><input id="txt_email" type="text" value="" placeholder="电子邮箱" /></td>   </tr>   <tr>    <td> </td>    <td><input id="btn_register" type="submit" value="提交注册" onclick="return confirm('是否确认提交注册');" /></td>   </tr>  </table> </form></body></html>            
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表