python上传文件方法总结

简介: python上传文件方法总结

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

相关文章
|
6天前
|
C++ 开发者 Python
实现Python日志点击跳转到代码位置的方法
本文介绍了如何在Python日志中实现点击跳转到代码位置的功能,以提升调试效率。通过结合`logging`模块的`findCaller()`方法记录代码位置信息,并使用支持点击跳转的日志查看工具(如VS Code、PyCharm),开发者可以从日志直接点击链接定位到出错代码,加快问题排查。
15 2
|
6天前
|
索引 Python
Python 中寻找列表最大值位置的方法
本文介绍了Python中找列表最大值及其位置的三种方法:1) 使用内置`max()`和`index()`函数;2) 通过循环遍历;3) 利用`enumerate()`函数和生成器表达式。每种方法均附有示例代码,其中`enumerate()`方法在保证效率的同时代码更简洁。
34 2
|
6天前
|
JSON 数据处理 数据格式
Python中批量提取[]括号内第一个元素的四种方法
Python中批量提取[]括号内第一个元素的四种方法
23 1
|
6天前
|
SQL 关系型数据库 数据库连接
使用 Python 访问数据库的基本方法
【5月更文挑战第12天】在Python中操作数据库涉及安装数据库驱动(如mysql-connector-python, psycopg2, pymongo)、连接数据库、执行查询/更新、处理结果集及关闭连接。使用ORM(如SQLAlchemy)可简化操作。通过上下文管理器(with语句)能更好地管理资源和错误。注意根据实际需求处理事务、错误和安全性,例如使用SSL连接。
24 2
|
1天前
|
数据采集 Python
2024年Python最新【Python基础教程】快速找到多个字典中的公共键(key)的方法,秋招面试问题
2024年Python最新【Python基础教程】快速找到多个字典中的公共键(key)的方法,秋招面试问题
2024年Python最新【Python基础教程】快速找到多个字典中的公共键(key)的方法,秋招面试问题
|
1天前
|
程序员 PHP Python
2024年Python最全Python基础教程:keys()、values()和 items()方法,百度面试题php
2024年Python最全Python基础教程:keys()、values()和 items()方法,百度面试题php
2024年Python最全Python基础教程:keys()、values()和 items()方法,百度面试题php
|
1天前
|
算法 开发工具 Python
python排序的几种方法(3)
python排序的几种方法(3)
|
1天前
|
算法 程序员 Python
python排序的几种方法(1)
python排序的几种方法(1)
|
1天前
|
SQL 分布式计算 算法
python-静态方法staticmethod、类方法classmethod、属性方法property_python staticmethod类内使用
python-静态方法staticmethod、类方法classmethod、属性方法property_python staticmethod类内使用
|
1天前
|
缓存 Java Python
python-静态方法staticmethod、类方法classmethod、属性方法property_python staticmethod类内使用(1)
python-静态方法staticmethod、类方法classmethod、属性方法property_python staticmethod类内使用(1)