目 录CONTENT

文章目录

python上传文件方法总结

懿曲折扇情
2022-05-16 / 2 评论 / 9 点赞 / 166 阅读 / 412 字 / 正在检测是否收录...
温馨提示:
本文最后更新于 2022-05-16,若内容或图片失效,请留言反馈。部分素材来自网络,若不小心影响到您的利益,请联系我们删除。
广告 广告

python上传文件方法总结

# 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)

注意:这个autoguiUpload()方法还没能上传成功,正在思考…emo

9

评论区