今天是一个表格的自动提交,进一步学习selenium的使用方法
练习的目标
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 )接受) :发送确定命令,相当于点击"确定"按钮
b ) dismiss () :取消操作相当于单击“取消”按钮,或单击右上角的“关闭”
c ) send _ keys :输入必须在prompt框中填写的内容
准备工作
html页面(注册页面,嵌入注册表; 这样列举例子是为了介绍练习selenium的switch_to_frame的使用方法)
1 )注册页面(路径d : (注册者demo (index.htm ) ) ) ) ) ) ) ) )
用户注册测试Python selenium自动提交表单2 )注册表(路径d : (注册者demo (register.htm ) ) ) ) ) ) )。
这是嵌入式表单input [ type=' text ' ] { border :1 px solid # ABC; font-size:14px; padding:5px; width:200px; }
input [ type=' password ' ] { border :1 px solid # ABC; font-size:14px; padding:5px; width:200px; }
输入[ type=' submit ' ] { border :1 px solid # ABC; font-size:14px; padding:5px 10px; width:100px; cursor:pointer; margin-top:20px; }
input [ type=' submit ' ] : hover { background-color : # AAAA ff; }
用户名:密码:电子邮件地址:
执行步骤
我们在Python IDLE上一步一步地执行。 这有助于理解,一步一步操作,惊喜不断
1 )部署selenium模块
来自selenium import web驱动程序
2 )启动firefox,读取注册页
bs=webdriver.Firefox (
bs.get (文件://d :/register demo/index.htm ) ) ) ) ) ) ) ) )。
3 )查找输入框(用户名、密码、电子邮件)和按钮),填写指定内容
#表单的内容嵌入在iframe中,因此必须找到指向iframe的指针
#如果还想退出iframe并返回父页面,请访问bs.switch_to_default_content (
bs.switch _ to _ frame (注册信息) )。
#所有元素都被指定了id,因此可以使用find_element_by_id。 还有很多其他的find_element_*可以练习
#找到用户名框,然后输入hertz.liu
account=bs.find _ element _ by _ id (' txt _ account ' ) ) ) ) ) ) ) ) )。
account.send_keys(Hertz.Liu ) )。
#找到密码框,输入“pwd123”
pwd=bs.find _ element _ by _ id (txt _ password ) ) ) )。
PWD.send_keys(pwd123 ) )。
#找到电子邮件地址框,然后输入hertz.liu@mail.com
email=bs.find _ element _ by _ id (txt _ email ) )。
email.send _ keys (hertz.Liu @ mail.com) )。
#查找发送按钮,模拟点击发送
BTN _ reg=bs.find _ element _ by _ id (BTN _ register ) )。
btn_reg.click (
4 )非常顺利,表格填写和提交完毕。 在典型表单中,由于涉及数据操作,开发人员将设置一些辅助确认以防止错误操作。 在此,我们使用简单的confirm进行二次确认。 此示例说明如何使selenium识别确认框,然后单击确定按钮
#将搜索对象移动到确认
confirm=bs.switch_to_alert (
点击#确定按钮
confirm.accept (
#如果要取消,请访问confirm.dismiss (
对于prompt,可以在调用accept (或dismiss )之前使用send_keys )输入内容
5 )关闭浏览器
bs.close () )
以上是编辑介绍的Python自动化表单提交的实例代码。 我希望对大家有帮助。 如果有问题的话请给我留言。 编辑马上回复大家。 感谢您为我查找教程网站!