Home >Backend Development >Python Tutorial >python automated form submission

python automated form submission

巴扎黑
巴扎黑Original
2017-06-23 15:11:032698browse

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=&#39;text&#39;]{border:1px solid #abc; font-size:14px; padding:5px; width:200px;}
		input[type=&#39;password&#39;]{border:1px solid #abc; font-size:14px; padding:5px; width:200px;}
		input[type=&#39;submit&#39;]{border:1px solid #abc; font-size:14px; padding:5px 10px; width:100px; cursor:pointer; margin-top:20px;}
		input[type=&#39;submit&#39;]: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(&#39;是否确认提交注册&#39;);" /></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 module

from selenium import webdriver
 2) Start firefox and load the registration page

bs = webdriver.Firefox()
bs.get(&#39;file:///D:/RegisterDEMO/index.htm&#39;)
 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(&#39;register-iframe&#39;)

# 由于所有的元素都命名了id,可以使用find_element_by_id,还有很多的其它find_element_*大家可以练习
# 查找用户名框,并填充“hertz.liu"
account = bs.find_element_by_id(&#39;txt_account&#39;)
account.send_keys(&#39;hertz.liu&#39;)

# 查找密码框,并填充"pwd123"
pwd = bs.find_element_by_id(&#39;txt_password&#39;)
pwd.send_keys(&#39;pwd123&#39;)

# 查找电子邮箱框,并填充”hertz.liu@mail.com"
email = bs.find_element_by_id(&#39;txt_email&#39;)
email.send_keys(&#39;hertz.liu@mail.com&#39;)

# 查找提交按钮,并模拟点击提交
btn_reg = bs.find_element_by_id(&#39;btn_register&#39;)
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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:day 1Next article:Python进阶之文件和流