# coding=utf-8
"""
@Project :pachong-master
@File :uploadFile.py
@Author :gaojs
@Date :2022/5/16 21:04
@Blogs : https://www.gaojs.com.cn
"""
from time import sleep
import pyautogui
from win32com.client import Dispatch
from pywinauto.keyboard import send_keys, SendKeys
from selenium import webdriver
import pywinauto
class UploadFile:
def __init__(self):
"""
上传文件
"""
self.driver = webdriver.Chrome()
self.driver.get('https://www.wenshushu.cn/')
self.driver.maximize_window()
self.driver.implicitly_wait(10)
def pywinUpload(self, filePath, filename):
"""
使用pywinauto上传
:return:
"""
self.driver.find_element_by_css_selector('div.chooice-btn button').click()
desk = pywinauto.Desktop()
# 点击打开按钮
frame = desk["打开"]
# 选择文件地址输入框
frame["Toolbar3"].click()
# 输入路径文件
send_keys(filePath)
# 键盘输入回车,打开该路径
send_keys("{VK_RETURN}")
# 选中文件名输入框,输入文件名
frame["文件名(&N):Edit"].type_keys(filename)
sleep(3)
# 点击打开
frame["打开(&O)"].click()
def upload(self, filenamePath):
"""
input标签才能这样:send_keys上传文件
:return:
"""
upload = self.driver.find_element_by_css_selector('div.chooice-btn button')
sleep(3)
# loadfile.send_keys(filenamePath)
upload.send_keys('filenamePath')
self.driver.find_element_by_id('onBtn')
def win32Upload(self, filenamePath):
"""
使用win32com.client上传文件
:param filenamePath:
:return:
"""
self.driver.find_element_by_css_selector('div.chooice-btn button').click()
patch = Dispatch("WScript.shell")
sleep(3)
patch.Sendkeys(filenamePath)
def autoguiUpload(self, filenamePath):
"""
pyautogui上传文件
:param filenamePath:
:return:
"""
self.driver.find_element_by_css_selector('div.chooice-btn button').click()
pyautogui.write(filenamePath)
sleep(3)
# pyautogui.keyDown('enter')
# pyautogui.keyUp('enter')
pyautogui.press('enter', presses=2)