1 from selenium.webdriver.support.ui import WebDriverWait
2 import configparser
3 import os
4 from selenium import webdriver
5 class getElement():
6 '''
7 从配置文件中来获取定位信息
8 '''
9 def __init__(self):
10 self.elementIni = os.path.dirname(os.path.abspath(__file__))\
11 +r'\WebElement.ini' # 配置文件所在目录
12 def getElement(self, driver, sogouSection, sogouOption):
13 try:
14 f = configparser.ConfigParser()
15 f.read(self.elementIni) # 读配置文件内容到内存中
16 locators = f.get(sogouSection, sogouOption).split(':')
17 # 获取定位方式
18 locaMethod = locators[0]
19 # 获取定位表达式
20 locaExpression = locators[1]
21 # 通过显示等待的方式获取页面的元素
22 element = WebDriverWait(driver,5).until(lambda x : x.find_element(locaMethod, locaExpression))
23 except Exception as e:
24 raise e
25 else:
26 return element
27
28 if __name__ == '__main__':
29 ele = getElement()
30 print(ele.elementIni)
31 driver = webdriver.Firefox()
32 driver.get('http://www.sogou.com')
33 element = ele.getElement(driver, 'sougou', 'queryBox')
34 element.send_keys('python')