Home >Backend Development >Python Tutorial >python automated form submission
Today I will use the automatic submission of a form to further learn the usage of selenium
Practice objectives
0 ) Use selenium to start firefox and load the specified page (you can view my article for this part)
1) Search for page elements (multiple search methods: find_element_*)
2) Content filling (send_keys) )
3) Switching between iframe and parent page (switch_to_frame is to switch to iframe, switch_to_default_content is to switch to the main page)
4) Browser interaction processing: window.alert, window.confirm, window .prompt
To interact with the above three browsers, you need to use switch_to_alert. There are several usages that need to be noted:
a) accept(): Send a confirmation instruction, which is equivalent to clicking " OK” button
b) dismiss(): Cancel the operation, which is equivalent to clicking the “Cancel” button or clicking “Close” in the upper right corner
c) send_keys: Fill in the content that needs to be filled in the prompt box
Preparation work
HTML page (registration page, embedded with a registration form; the reason for this example is to Introducing the usage of switch_to_frame for practicing selenium)
1) Registration page (path 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) Registration form (path 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>
##Running steps
We run it step by step through Python IDLE, which is helpful for understanding. One step at a time, there are constant surprises 1) Introduce selenium modulefrom selenium import webdriver2) Start firefox and load the registration page
bs = webdriver.Firefox() bs.get('file:///D:/RegisterDEMO/index.htm')3) Find the input box (user name, password , email) and button (submit registration), and fill in the specified content
# 由于表单内容是嵌在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()4) Very smoothly, the filling and submission of the form was completed. For general forms, since they involve data operations, developers will set up some secondary confirmations to prevent misoperations. Here a simple confirm is used for secondary confirmation. Here is how to let selenium recognize the confirm box and click the "OK" button
# 将查找对象转移至confirm confirm = bs.switch_to_alert() # 点击确定按钮 confirm.accept() # 如果要取消,使用confirm.dismiss() # 如果是prompt,则可以使用send_keys()先填充内容,再调用accept()或dismiss()5) Close the browser
bs.close()
The above is the detailed content of python automated form submission. For more information, please follow other related articles on the PHP Chinese website!