# 导入库 import os from selenium import webdriver from selenium.webdriver.common.keys import Keys from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.common.by import By from selenium.webdriver.support.wait import WebDriverWait import time import pandas as pd # 账号密码 -- 需要修改 name = '15888265981' password = 'F6123456' driver = webdriver.Chrome(executable_path=r"C:\Users\admin\Desktop\Python脚本\chromedriver.exe") driver.maximize_window() # 最大化浏览器窗口 real_https = 'https://yunxiu.f6car.cn/' # 正式网址 fake_https = 'https://m-trial.f6car.cn/' # 测试网址 # 打开网页,根据情况更改网址变量 driver.get(real_https) # 在输入框中输入账号和密码 username = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, r'//*[@id="pane-username"]/form/div[1]/div/div[1]/input'))) enter = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, r'//*[@id="pane-username"]/form/div[3]/div/button'))) username.clear() username.send_keys(name) driver.find_element_by_xpath(r'//*[@id="pane-username"]/form/div[2]/div/div[1]/input').send_keys(password) enter.click() # 等待一点时间,手动选择门店 time.sleep(5) # 点击基础资料下拉菜单 WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, r'//*[@id="user-nav"]/ul/li[3]/a/span'))).click() # 点击 数据导入 菜单项 WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, r'//*[@id="user-nav"]/ul/li[3]/ul/li[17]/a'))).click() # 点击 套餐卡信息 WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, r'//*[@id="content"]/div/div/ul/li[5]/a'))).click() # 进入框架 iframe -- weixin-page-iframe ,方法1,可行(原等待时间 2 2 1) time.sleep(0.5) iframe = driver.find_element_by_xpath(r'//*[@id="weixin-page-iframe"]') time.sleep(1) driver.switch_to.frame(iframe) time.sleep(0.5) print('已进入iframe 框架') files_path = r'D:\F6_data\套餐卡导入测试数据\批量导入3' # 开始批量导入 for file in os.listdir(files_path): try: # 找到按钮里的 input 标签 to_input = driver.find_element_by_xpath(r'//*[@id="app"]/div/div/div/div[2]/form/div[1]/div/div/div/div[1]/input') time.sleep(0.2) upload_file = os.path.join(files_path,file) to_input.send_keys(upload_file) time.sleep(1) # print(f'已经上传excel文件:{file}!') # 点击 立即导入按钮 WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, r'//*[@id="app"]/div/div/div/div[2]/form/div[1]/div/button/span'))).click() # 点击 查看导入记录 按钮 WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, r'//*[@id="app"]/div/div/div/div[2]/form/div[2]/div/div[1]'))).click() # 检查文件名 upload_file_name = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, r' //*[@id="app"]/div/div/div/div[3]/div/div[2]/div[1]/div[3]/table/tbody/tr[1]/td[1]/div'))).text time.sleep(0.5) while upload_file_name != file: # 关闭再打开 导入记录 WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, r'//*[@id="app"]/div/div/div/div[3]/div/div[1]/button/i'))).click() # 关闭按钮 time.sleep(1) WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, r'//*[@id="app"]/div/div/div/div[2]/form/div[2]/div/div[1]'))).click() time.sleep(1) upload_file_name = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, r' //*[@id="app"]/div/div/div/div[3]/div/div[2]/div[1]/div[3]/table/tbody/tr[1]/td[1]/div'))).text print('导入文件已经进入导入记录中!') # 打印卡导入 状态 信息 succeed_cards = driver.find_element_by_xpath(r'//*[@id="app"]/div/div/div/div[3]/div/div[2]/div[1]/div[3]/table/tbody/tr[1]/td[3]/div').text time.sleep(0.5) lost_cards = driver.find_element_by_xpath(r'//*[@id="app"]/div/div/div/div[3]/div/div[2]/div[1]/div[3]/table/tbody/tr[1]/td[4]/div').text time.sleep(0.5) print(f'成功卡数:{succeed_cards},失败卡数:{lost_cards}') while succeed_cards == '' or lost_cards == '': # 不断的关闭打开 导入记录 ,刷新页面 WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, r'//*[@id="app"]/div/div/div/div[3]/div/div[1]/button/i'))).click() # 关闭按钮 time.sleep(1) WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, r'//*[@id="app"]/div/div/div/div[2]/form/div[2]/div/div[1]'))).click() # 导入记录 time.sleep(1) succeed_cards = driver.find_element_by_xpath(r'//*[@id="app"]/div/div/div/div[3]/div/div[2]/div[1]/div[3]/table/tbody/tr[1]/td[3]/div').text time.sleep(1) lost_cards = driver.find_element_by_xpath(r'//*[@id="app"]/div/div/div/div[3]/div/div[2]/div[1]/div[3]/table/tbody/tr[1]/td[4]/div').text time.sleep(1) print(f'succeed_cards:{succeed_cards},lost_cards:{lost_cards}',end='-') print() print('#######################可以导入下一个文件!#######################') # 关闭 导入记录 窗口 WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, r'//*[@id="app"]/div/div/div/div[3]/div/div[1]/button/i'))).click() except Exception as e : print(f'{file} 导入出错') print(repr(e)) continue finally: time.sleep(600) # 如果时间足够的话,保险起见可以多等一会 print('所有文件上传完成!') time.sleep(1) # driver.close()