Learn more about selenium with a form autosubmit today!
Exercise Objectives
0) Use selenium to start firefox and load the specified page (this part can be viewed in my article)/liu2008hz/p/)
1) Page element lookup (multiple lookups: find_element_*)
2) Content filling (send_keys)
3) iframe and parent page switching (switch_to_frame is switching to iframe, switch_to_default_content is switching to main page)
4) Browser Interaction Processing:, , ,
Interacting with the content of the three browsers above requires the use of switch_to_alert, and there are a few usages to be aware of:
a) accept(): send the OK command, equivalent to clicking the "OK" button
b) dismiss(): cancel the operation, equivalent to clicking on the "Cancel" button or clicking on the upper-right corner of the "Close"
c) send_keys: fill in the content required for the prompt box
preliminary
html page (a registration page with a registration form embedded; the reason for this example is to introduce the use of switch_to_frame for practicing selenium)
1) Registration page (path D:\RegisterDEMO\)
<!DOCTYPE> <html> <head> <title>User Registration</title> <meta charset="utf-8" /> </head> <body> <h3>test (machinery etc)Python seleniumAutomatic form submission</h3> <iframe width="320" height="200" border="0" src="" /> </body> </html>
2) Registration form (path D:\RegisterDEMO\)
<!DOCTYPE> <html> <head> <title>This is the inline form</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>user ID:</td> <td><input type="text" value="" placeholder="Username" /></td> </tr> <tr> <td>cryptographic:</td> <td><input type="password" value="" placeholder="Password." /></td> </tr> <tr> <td>e-mail address:</td> <td><input type="text" value="" placeholder="E-Mail" /></td> </tr> <tr> <td> </td> <td><input type="submit" value="Submit Registration" onclick="return confirm('Confirmation of submission of registration');" /></td> </tr> </table> </form> </body> </html>
operating procedure
Let's run through Python IDLE step by step, it helps to understand, one operation at a time, the surprises keep coming!
1) Introduce the selenium module
from selenium import webdriver
2) Launch firefox and load the registration page
bs = () ('file:///D:/RegisterDEMO/')
3) Find the input boxes (username, password, email) and buttons (submit registration) and fill in the specified content
# Since the content of the form is embedded in an iframe, you need to look for a pointer to the iframe # If you want to jump out of the iframe again and go back to the parent page, you can use bs.switch_to_default_content() bs.switch_to_frame('register-iframe') # Since all elements name ids, you can use find_element_by_id, and there are many other find_element_* you can practice with # Find the username box and fill it with "" account = bs.find_element_by_id('txt_account') account.send_keys('') # Find the password box and fill it with "pwd123". pwd = bs.find_element_by_id('txt_password') pwd.send_keys('pwd123') # Find the e-mail box and fill it with "@" email = bs.find_element_by_id('txt_email') email.send_keys('@') # Find the submit button and simulate clicking submit btn_reg = bs.find_element_by_id('btn_register') btn_reg.click()
4) very smoothly, completed the form filling and submission. General form, because it involves the operation of data, developers will set up some secondary confirmation to prevent misuse. Here is the use of a simple confirm to the second confirmation, the following is how to let selenium to recognize the confirm box, and click the "OK" button
# Transfer the lookup to confirm confirm = bs.switch_to_alert() # Click the OK button () # If you want to cancel, use () # If it isprompt,Then you can use thesend_keys()Fill in the content first,re-callaccept()maybedismiss()
5) Close the browser
()
The above is a small introduction to the Python automated form submission example code, I hope to help you, if you have any questions please leave me a message, I will promptly reply to you. I would also like to thank you very much for your support of my website!