selenium 元素定位+显示等待 方法封装

#coding:utf-8

from selenium import webdriver

from selenium.webdriver.support.ui import WebDriverWait

def getElement(driver,locateType,locatorExpression):
    try:
        element=WebDriverWait(driver,30).until(lambda x:x.find_element(by=locateType,value=locatorExpression))
        return element
    except Exception,e:
        print e


def getElements(driver,locateType,locatorExpression):
    try:
        elements=WebDriverWait(driver,30).until(lambda x:x.find_elements(by=locateType,value=locatorExpression))
        return elements
    except Exception,e:
        print e


"""测试"""
driver=webdriver.Firefox()
driver.get("http://www.baidu.com/")

#定位单个元素,并输入内容
getElement(driver,"id","kw").send_keys("ceshi")

#定位多个元素,并返回元素的文本内容
a=getElements(driver,"class name","mnav")
for i in a:
    print i.text

 

posted @ 2018-06-20 14:03  大米粒粒  阅读(602)  评论(0编辑  收藏  举报