场景
测试对象的定位和操作是webdriver的核心内容,其中操作又是建立在定位的基础之上,因此对象定位就越发显得重要了。
定位对象的目的一般有下面几种
- 操作对象
- 获得对象的属性,如获得测试对象的class属性,name属性等等
- 获得对象的text
- 获得对象的数量
webdriver提供了一系列的对象定位方法,常用的有以下几种
- id
- name
- class name
- link text
- partial link text
- tag name
- xpath
- css selector
代码
login.html
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title></title> <link rel="stylesheet" href="/static/plugins/bootstrap/css/bootstrap.css"/> <link rel="stylesheet" href="/static/plugins/font-awesome/css/font-awesome.css"/> <link rel="stylesheet" href="/static/css/edmure.css"/> <link rel="stylesheet" href="/static/css/commons.css"/> <link rel="stylesheet" href="/static/css/account.css"/> </head> <body> <div class="login"> <form id="fm" method="POST" action="/login.html"> {% csrf_token %} <div class="form-group"> <label for="username">用户名</label> <input type="text" class="form-control" name="username" id="username" placeholder="请输入用户名"> </div> <div class="form-group"> <label for="password">密码</label> <input type="password" class="form-control" name="password" id="password" placeholder="请输入密码"> </div> <div class="form-group"> <label for="password">验证码</label> <div class="row"> <div class="col-xs-7"> <input type="text" class="form-control" name="check_code" id="check_code" placeholder="请输入验证码"> </div> <div class="col-xs-5"> <img id="check_code_img" src="/check_code.html"> </div> </div> </div> <div class="checkbox"> <label> <input type="checkbox" value="1" name="rmb"> 一个月内自动登陆 </label> <div class="right"> <a href="#">忘记密码?</a> </div> </div> <div class="row"> <div class="col-xs-3"> <a id="submit" class="btn btn-default">登 陆</a> </div> <div class="col-xs-9" style="padding-left: 0;"> <div class="alert alert-danger hide"> <span style="padding: 0 5px 0 5px;display: inline-block;font-size: 14px"> <i class="fa fa-minus-circle" aria-hidden="true"></i> </span> <span id="error_msg" style="font-size: 12px;"></span> </div> </div> </div> </form> <script src="/static/js/jquery-1.12.4.js"></script> <script type="text/javascript"> $(function () { bindLogin(); }); function bindLogin() { $(‘#submit‘).click(function () { var $msg = $(‘#error_msg‘); $msg.parent().addClass(‘hide‘); $.ajax({ url: ‘/login.html‘, type: ‘POST‘, data: $(‘#fm‘).serialize(), dataType: ‘JSON‘, success: function (arg) { if(arg.status){ location.href = ‘/‘ }else{ $msg.parent().removeClass(‘hide‘); $msg.text(arg.message); var img = $(‘#check_code_img‘)[0]; img.src = img.src + ‘?‘; $(‘#password,#check_code‘).val(‘‘); } } }) }) } </script> </div> </body> </html>
python代码
#!/usr/bin/env python # -*- coding:utf-8 -*- ‘‘‘ Created on 2018/5/9 11:35 @author: Jeff Lee @file: 简单的对象定位.py ‘‘‘ 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(‘login.html‘) print (file_path) dr.get(file_path) # by id print(‘通过id进行定位‘) dr.find_element_by_id(‘username‘).click() sleep(1) # by name print(‘通过name进行定位‘) dr.find_element_by_name(‘password‘).click() sleep(1) # by tagname print(‘通过tag进行定位‘) print (dr.find_element_by_tag_name(‘form‘).get_attribute(‘id‘)) sleep(1) # by class_name print(‘通过class进行定位‘) e = dr.find_element_by_class_name(‘form-control‘) try: dr.execute_script(‘$(arguments[0]).fadeOut().fadeIn()‘, e) except Exception as e: print(‘没有JS‘) sleep(1) # by link text print(‘通过link文本进行定位‘) link = dr.find_element_by_link_text(‘忘记密码?‘) try: dr.execute_script(‘$(arguments[0]).fadeOut().fadeIn()‘, link) except Exception as e: print(‘没有JS‘) sleep(1) # by partial link text print(‘通过 部分link文本 进行定位‘) link = dr.find_element_by_partial_link_text(‘忘记‘) try: dr.execute_script(‘$(arguments[0]).fadeOut().fadeIn()‘, link) except Exception as e: print(‘没有JS‘) sleep(1) # by css selector #print(‘通过 css selector 进行定位‘) #div = dr.find_element_by_css_selector(‘.controls‘) #dr.execute_script(‘$(arguments[0]).fadeOut().fadeIn()‘, div) #sleep(1) # by xpath print(‘通过 xpath 进行定位‘) dr.find_element_by_xpath(‘/html/body/div/form/div[3]/div/div/input‘).click() sleep(2) dr.quit()
讨论
上面例子里由于html文件中引用了jquery,所以在执行js时可以使用jquery的$()及fadeIn()等方法。如果你测试的页面没用包含jquery的话,这些方法是无效的。
原文地址:https://www.cnblogs.com/uniquefu/p/9017999.html
时间: 2024-11-05 15:43:34