【python】 自动化上传小程序
from selenium import webdriver import win32gui import win32con from selenium.webdriver.common.by import By from selenium.webdriver.remote.webelement import WebElement from selenium.webdriver.common.action_chains import ActionChains import time driver = webdriver.Chrome() url = "http://192.168.1.187/index.html" try: driver.get(url) driver.find_element_by_id("username").clear() driver.find_element_by_id('username').send_keys("12345678") driver.find_element_by_id('password').clear() driver.find_element_by_id('password').send_keys("12345678") driver.find_element_by_id('loginBtn').click() time.sleep(1) driver.find_element_by_id('configurefile-li').click() time.sleep(1) upload = driver.find_element_by_id('add') def get_path(path, wait): upload.click() time.sleep(1) dialog = win32gui.FindWindow('#32770', u'打开') ComboBoxEx32 = win32gui.FindWindowEx(dialog, 0, 'ComboBoxEx32', None) ComboBox = win32gui.FindWindowEx(ComboBoxEx32, 0, 'ComboBox', None) Edit = win32gui.FindWindowEx(ComboBox, 0, 'Edit', None) button = win32gui.FindWindowEx(dialog, 0, 'Button', u"打开(&O)") win32gui.SendMessage(Edit, win32con.WM_SETTEXT, None, path) win32gui.SendMessage(dialog, win32con.WM_COMMAND, 1, button) time.sleep(1) driver.find_element_by_id('upload').click() time.sleep(wait) driver.find_element_by_class_name('jconfirm-buttons').click() time.sleep(3) path1 = u'd:\\path1' path2 = u'd:\\path2' path3 = u'd:\\path3' path4 = u'd:\\path4' path5 = u'd:\\path5' path6 = u'd:\\path6' get_path(path1, 10) get_path(path2, 5) get_path(path3, 5) get_path(path4, 10) get_path(path5, 10) get_path(path6, 30) time.sleep(10) driver.quit() except: print("The module is power off!") time.sleep(10) driver.quit()
代码解析:
#selenium 网页驱动
#win32gui windows自动化控件
#win32con windows控件
from selenium import webdriver
import win32gui
import win32con
from selenium.webdriver.common.by import By
from selenium.webdriver.remote.webelement import WebElement
from selenium.webdriver.common.action_chains import ActionChains