Python脚本控制的WebDriver 常用操作 <二十> 处理表单元素

本文提供了一个使用Python和Selenium WebDriver的示例代码,用于操作HTML表单中的输入框、单选按钮、复选框、下拉列表和提交按钮。通过演示如何使用send_keys方法赋值、click方法触发事件,帮助开发者理解和实践表单元素的基本交互。

 

测试用例场景


   表单对象的操作比较简单,只需要记住下面几点

  • 使用send_keys方法往多行文本框和单行文本框赋值;
  • 使用click方法选择checkbox
  • 使用click方法选择radio
  • 使用click方法点击button
  • 使用click方法选择option,从而达到选中select下拉框中某个具体菜单项的效果

 

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()

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

转载于:https://www.cnblogs.com/wuzhiming/p/3480431.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值