Pythonスクリプト制御のWebDriver通常操作処理フォーム要素
13405 ワード
テストケースシーン
フォームオブジェクトの操作は簡単です.次の点だけを覚えてください.
Pythonスクリプト
テスト用HTMLコード:
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<title>form</title>
<script type="text/javascript" async="" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<link href="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/css/bootstrap-combined.min.css" rel="stylesheet" />
<script type="text/javascript">
$(document).ready(function(){
$('input[type=submit]').click(function(){
alert('watir-webdriver is better than selenium webdriver');
});
});
</script>
</head>
<body>
<h3>form</h3>
<div class="row-fluid">
<div class="span6 well">
<form>
<fieldset>
<legend>Legend</legend>
<label class="checkbox">
<input type="checkbox"> Check me out
</label>
<label class="radio">
<input type="radio"> select me
</label>
<label class="select">
<select>
<option>0</option>
<option>1</option>
<option>2</option>
</select> select one item
</label>
<input type="submit" class="btn" value="submit" />
</fieldset>
</form>
</div>
</div>
</body>
<script src="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/js/bootstrap.min.js"></script>
</html>
テスト用Pythonコード:
# coding=gbk
'''
Created on 2013 12 18
@author: Administrator
'''
from selenium import webdriver
from time import sleep
import os
if 'HTTP_PROXY' in os.environ: del os.environ['HTTP_PROXY']
dr = webdriver.Firefox()
file_path = 'file:///' + os.path.abspath('formtest.html')
dr.get(file_path)
# checkbox
dr.find_element_by_css_selector('input[type=checkbox]').click()
sleep(5)
# radio
dr.find_element_by_css_selector('input[type=radio]').click()
sleep(5)
#
dr.find_element_by_class_name('select').find_elements_by_tag_name('option')[-2].click()
sleep(5)
#
dr.find_element_by_css_selector('input[type=submit]').click()
sleep(5)
dr.quit()