上一篇介绍了对于浏览器请求的封装,这一片主要时对于元素定位的方法进行封装。
import os
from selenium import webdriver
from selenium.webdriver.common.by import By
from config.szcs_driver import get_firefox_driver
from config.read_ini import sczsconfig
class sczsindElement():
def __init__(self,driver):
self.driver = driver
def get_elementy(self,file=None,node=None,key=None):
if file == None:
file = os.path.dirname(os.path.dirname(__file__))+"/bussin/LocalElement.ini"
if node == None:
node = "sczslogn_fatie"
read_ini = sczsconfig(file,node)
data = read_ini.get_value(key)
by = data.split('>')[0]
value = data.split('>')[1]
try:
if by == 'id':
return self.driver.find_element(By.ID,value)
elif by == 'class_name':
return self.driver.find_element(By.CLASS_NAME,value)
elif by == 'xpath':
return self.driver.find_element(By.XPATH,value)
elif by == 'tag_name':
return self.driver.find_element(By.TAG_NAME,value)
except:
return None
if __name__ == '__main__':
driver = get_firefox_driver()
sczs = sczsindElement(driver)
print(sczs.get_elementy(key="morenbankuai"))
拿走不谢,留下一个小心心呗。