很弱智的小脚本,记录下。也许以后看到会笑,因为太幼稚或者证明曾经也努力过。so...
这次厉害了,厉害了....
from selenium import webdriver # 导入封装的Logger from com.learn_logging.logs_test import Logger # 显示等待 from selenium.webdriver.support.wait import WebDriverWait # 鼠标事件 from selenium.webdriver.common.action_chains import ActionChains # 生成随机数字 import random # 所有异常类 from selenium.common.exceptions import * # 键盘模块 from selenium.webdriver.common.keys import Keys import time from selenium import webdriver # 导入日志模块 from com.learn_logging.logs_test import Logger #显示等待 from selenium.webdriver.support.wait import WebDriverWait # 鼠标事件 from selenium.webdriver.common.action_chains import ActionChains # 随机价格 import random # 所有异常 from selenium.common.exceptions import * # 键盘模块 from selenium.webdriver.common.keys import Keys import time class Item: def __init__(self): # 初始化browser self.browser = webdriver.Chrome() self.url = 'http://xxxxxxxxxxx/login.jsp' self.browser.get(self.url) self.browser.implicitly_wait(10) self.browser.maximize_window() def Clear(self): # 清空输入框 self.browser.find_element_by_id('tenentid').clear() self.browser.find_element_by_id('loginUserName').clear() self.browser.find_element_by_id('password').clear() def Loggin(self, tenant, username, password): # 参数化商户号、用户名、密码 self.browser.find_element_by_id('tenentid').send_keys(tenant) self.browser.find_element_by_id('loginUserName').send_keys(username) self.browser.find_element_by_id('password').send_keys(password) self.browser.find_element_by_id('login').click() def Check(self): # 检查登录是否成功 t = WebDriverWait(self.browser, 10, 1).until(lambda x: x.find_element_by_xpath( "//ul[@id='first_nav']/a[2]").get_attribute('text').strip()) if t == '源数据管理': Logger().log().info('登录成功^_^') else: Logger().log().info('登录失败T_T') def Go_item(self): # 点击源数据管理 WebDriverWait(self.browser, 10, 1).until(lambda x: x.find_element_by_xpath( "//ul[@id='first_nav']/a[2]/span")).click() # 点击运营资料设置 WebDriverWait(self.browser, 10, 1).until(lambda x: x.find_element_by_xpath( "//div[@id='_f2001']/a/span/span")).click() # 鼠标事件,调用封装函数 self.Find_element(time.time()) def Random_add_item(self): # 切换iframe self.browser.switch_to_frame('iframe2167') WebDriverWait(self.browser, 10, 1).until(lambda x: x.find_element_by_xpath( "//ul[@id='menu_tree']/li/ul/li/ul/li/div/span[5]")).click() WebDriverWait(self.browser, 10, 1).until(lambda x: x.find_element_by_xpath( "//*[@id='_add']/span/span[1]")).click() try: file_name = 'item_names.txt' with open(file_name, encoding='gbk') as f_obj: for date in f_obj: price = random.randint(1, 10) date = date.strip() # 菜品名称 n = WebDriverWait(self.browser, 10, 1).until(lambda x:x.find_element_by_xpath( "//*[@id='ff']/table/tbody/tr[1]/td[6]/span/textarea")) n.clear() n.send_keys(Keys.BACKSPACE) n.send_keys(date) # 菜品价格 WebDriverWait(self.browser, 10, 1).until(lambda x: x.find_element_by_xpath( "//*[@id='tt123']/div[1]/div[3]/ul/li[2]/a/span[1]")).click() # 判断是否已经存在菜品价格数据 price_type = self.browser.find_element_by_xpath('//*[@id="datagrid-row-r4-2-0"]/td[7]/div').text if price_type == '有效': # 编辑 //*[@id="datagrid-row-r4-2-0"]/td[7]/div WebDriverWait(self.browser, 10, 1).until(lambda x: x.find_element_by_xpath( "//*[@id='datagrid-row-r4-2-0']/td[3]/div/div/span[1]")).click() self.browser.find_element_by_xpath( "//*[@id='datagrid-row-r4-2-0']/td[5]/div/table/tbody/tr/td/span/input[1]").clear() WebDriverWait(self.browser, 10, 1).until(lambda x: x.find_element_by_xpath( "//*[@id='datagrid-row-r4-2-0']/td[4]/div/table/tbody/tr/td/span/input[1]")).send_keys('例') self.browser.find_element_by_xpath( "//*[@id='datagrid-row-r4-2-0']/td[5]/div/table/tbody/tr/td/span/input[1]").send_keys(price) else: # 新增 WebDriverWait(self.browser, 10, 1).until(lambda x: x.find_element_by_xpath( "//*[@id='edit_add1']/span/span[1]")).click() WebDriverWait(self.browser, 10, 1).until(lambda x: x.find_element_by_xpath( "//*[@id='datagrid-row-r4-2-0']/td[4]/div/table/tbody/tr/td/span/input[1]")).send_keys('例') WebDriverWait(self.browser, 10, 1).until(lambda x: x.find_element_by_xpath( "//*[@id='datagrid-row-r4-2-0']/td[5]/div/table/tbody/tr/td/span/input[1]")).send_keys(price) # 保存价格 WebDriverWait(self.browser, 10, 1).until(lambda x: x.find_element_by_xpath( "//*[@id='datagrid-row-r4-2-0']/td[3]/div/div/span")).click() # 保存并继续 self.browser.find_element_by_xpath("//*[@id='btn_save_goon']/span/span").click() # 菜品名称已经存在异常提示 time.sleep(2) item_name = self.browser.find_element_by_xpath('/html/body/div[26]/div[2]') # y1 = item_name.is_displayed() if item_name.text == '菜品名称已存在!': Logger().log().critical('保存菜品失败,原因:%s' % item_name.text) Logger().log().critical('保存失败的菜品名称是:%s' % date) y1 = item_name.is_displayed() if y1: # 关闭错误提示 self.browser.find_element_by_xpath('/html/body/div[26]/div[1]/div[2]/a').click() continue else: continue # 保存成功提示框 WebDriverWait(self.browser, 10, 1).until(lambda x: x.find_element_by_xpath( "//*[contains(text(),'确定')]")).click() except FileNotFoundError as e: Logger().log().critical('文件没有找到%s' % e) except UnicodeDecodeError as e: Logger().log().critical('Unicode解码错误:%s' % e) def Find_element(self,starTime): # 加入时间判断,避免死循环 intervalTime = 10 now = time.time() if now - starTime > intervalTime: return element = self.browser.find_element_by_xpath("//ul[@id='third_nav_a']/li[3]/a/span") ActionChains(self.browser).move_to_element(element).perform() time.sleep(2) element1 = self.browser.find_element_by_xpath("//div[@id='_t2043']/a[3]/span") ActionChains(self.browser).move_to_element(element1).perform() y = element1.is_displayed() Logger().log().info('界面元素是否可见?%s' % y) try: if y: WebDriverWait(self.browser, 10, 1).until(lambda x:x.find_element_by_xpath( "//div[@id='_t2043']/a[3]/span")).click() # element1.click() else: # 回调 self.Find_element(starTime) except ElementNotVisibleException as e: Logger().log().critical('鼠标事件点击失败:%s' % e) def Quit(self): self.browser.quit() def Run(self): self.Clear() self.Loggin('xx', 'xx', 'xx') self.Check() self.Go_item() self.Random_add_item() self.Quit() return self.browser if __name__ == '__main__': Item().Run()