<!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>
<!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>
from selenium import webdriver
bs = webdriver.Firefox()
bs.get('file:///D:/RegisterDEMO/index.htm')
# 由于表单内容是嵌在iframe里的,所以需要查找指向至iframe
# 如果又想跳出iframe,回到父页面,可以使用 bs.switch_to_default_content()
bs.switch_to_frame('register-iframe')
# 由于所有的元素都命名了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()
# 将查找对象转移至confirm confirm = bs.switch_to_alert() # 点击确定按钮 confirm.accept() # 如果要取消,使用confirm.dismiss() # 如果是prompt,则可以使用send_keys()先填充内容,再调用accept()或dismiss()
bs.close()
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有