Python automated form submission instance code, python instance
The automatic submission of a form is used today to learn more about selenium usage.
Exercise Objectives
0) Use selenium to start firefox and load the specified page (this part can be viewed in my article http://www.cnblogs.com/liu2008hz/p/6958126.html)
1) page element search (multiple search methods: find_element _*)
2) content filling (send_keys)
3) switching between iframe and parent page (switch_to_frame switches to iframe, while switch_to_default_content switches to the home page)
4) browser interaction processing: window. alert, window. confirm, window. prompt
Use switch_to_alert to interact with the preceding three browsers. Note the following usage:
A) accept (): sends a confirmation command, which is equivalent to clicking "OK ".
B) dismiss (): canceling an operation is equivalent to clicking the "cancel" button or clicking "close" in the upper right corner"
C) send_keys: content to be filled in the prompt box
Preparations
Html page (registration page, embedded with a registry ticket; for example, this is to introduce the use of switch_to_frame in selenium)
1) registration page (path D: \ RegisterDEMO \ index.htm)
<! DOCTYPE>
2) Registry ticket (path D: \ RegisterDEMO \ register.htm)
<! DOCTYPE>
Procedure
We use Python IDLE to run it step by step, which helps us to understand, step by step, and is always pleasantly surprised.
1) Introduce selenium Module
from selenium import webdriver
2) Start firefox and load the registration page
bs = webdriver.Firefox()bs.get('file:///D:/RegisterDEMO/index.htm')
3) Search for the input box (user name, password, email) and button (submit registration), and fill in the specified content
# Because the form content is embedded in iframe, you need to find and point to iframe # If you want to jump out of iframe and return to the parent page, you can use bs. switch_to_default_content () bs. switch_to_frame ('register-iframe') # Because all elements are named IDs, you can use find_element_by_id. There are many other find_element _ * exercises # search for the username box, and fill in "hertz. liu "account = bs. find_element_by_id ('txt _ account') account. send_keys ('hertz. liu ') # search for the password box and fill in "pwd123" pwd = bs. find_element_by_id ('txt _ password') pwd. send_keys ('pwd123') # Find the email box and fill in the "hertz.liu@mail.com" email = bs. find_element_by_id ('txt _ email ') email. send_keys ('hertz. liu@mail.com ') # Find the submit button and simulate clicking submit btn_reg = bs. find_element_by_id ('btn _ register ') btn_reg.click ()
4) The filling and submission of the form were completed smoothly. Generally, because data operations are involved, developers will set up secondary confirmation to prevent misoperation. The simple confirm is used for Secondary confirmation. The following describes how to let selenium identify the confirm box and click "OK ".
# Transfer the search object to confirmconfirm = bs. switch_to_alert () # click confirm. accept () # Use confirm. dismiss () # if it is a prompt, you can use send_keys () to fill in the content first, and then call accept () or dismiss ()
5) Close the browser
bs.close()
The above is the Python automated form submission example Code introduced by the editor. I hope it will be helpful to you. If you have any questions, please leave a message and the editor will reply to you in time. Thank you very much for your support for the help House website!