#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