Python_selenium中类函数模块的简单介绍

Python_selenium中类函数模块的简单介绍

一、demo1.py的代码如下所示

#coding:utf-8

class ClassA(object):

    string = "这是一个字符串"

    def instancefunc(self):

        print "这是一个实例方法"

        print self

    @classmethod

    def classfuc(cls):

        print "这是一个类方法"

        print cls

    @staticmethod

    def staticfuc():

        print '这是一个静态方法'

test=ClassA()  #初始化一个ClassA的对象,test是实例ClassA的实例对象

test.instancefunc()#对象调用实例方法

test.staticfuc()#对象调用静态方法

test.classfuc()#对象调用类方法

print test.string#对象调用类变量

ClassA.instancefunc(test)#类调用实例方法,需要带参数,test为实例参数

ClassA.instancefunc(ClassA)#类调用实例方法,需要带参数,ClassA为类参数

ClassA.staticfuc()#类调用静态方法

ClassA.classfuc()#类调用类方法

二、备注

1. 类的定义,class开头的就表示这是一个类,小括号里面的,表示这个类的父类,涉及到继承,默认object是所有类的父类。python中定义类

小括号内主要有三种:1. 具体一个父类,2. object 3. 空白

2. 函数或方法的定义, def开头就表示定义一个函数,方法包括,实例方法,类方法,静态方法,注意看类方法和静态方法定义的时候上面有一个@标记。

3. 对象调用方法和类调用方法的使用。

三、实际测试脚本

#coding:utf-8

from selenium import webdriver

import time

class BaiduSearch(object):

    driver=webdriver.Firefox()

    driver.maximize_window()

    driver.implicitly_wait(8)

    def open_baidu(self):

        self.driver.get("https://www.baidu.com/")

        time.sleep(2)

    def test_search(self):

        self.driver.find_element_by_id("kw").send_keys("selenium")

        time.sleep(2)

        print self.driver.title

        try:

            assert 'selenium' in self.driver.title

            print 'test pass'

        except Exception as e:

            print 'test fail'

        self.driver.quit()

baidu=BaiduSearch() #初始化一个BaiduSearch的对象,baidu是BaiduSearch的实例对象

baidu.open_baidu()#对象调用实例方法

baidu.test_search()#对象调用实例方法

详情参考:http://blog.csdn.net/u011541946/article/details/70157011

原文地址:https://www.cnblogs.com/Rita-LJ/p/8079040.html