当前位置:首页 > python > 正文内容

用python写个selenium上传文件的程序,非input标签的上传

zhangsir3年前 (2023-03-24)python318

非input标签的上传通常是通过模拟点击“上传”按钮打开系统的文件选择框,然后在文件选择框中选择文件并点击“确定”按钮实现的。在Selenium中,我们可以使用AutoIT或pywinauto等工具来模拟操作系统的窗口,实现非input标签的上传。


下面是一个使用pywinauto模块实现非input标签上传文件的示例程序:


from selenium import webdriver
from pywinauto.application import Application
from pywinauto.keyboard import send_keys
# 指定要上传的文件路径
file_path = r"C:\path\to\your\file.jpg"
# 创建WebDriver对象,这里使用Chrome浏览器作为示例
driver = webdriver.Chrome()
# 打开要上传文件的页面
driver.get("https://example.com/upload")
# 找到上传按钮并点击
upload_button = driver.find_element_by_xpath("//button[@class='upload-button']")
upload_button.click()
# 使用pywinauto模块模拟系统的文件选择框
app = Application(backend='uia').connect(title='打开')  # 打开系统的文件选择框
app['打开']['文件名(N):Edit'].set_text(file_path)  # 将要上传的文件路径填入文件名输入框
send_keys("{ENTER}")  # 模拟按下回车键,选择文件并点击“确定”按钮
# 等待上传完成
# ...
# 关闭浏览器
driver.quit()

在这个示例中,我们使用了pywinauto模块模拟了系统的文件选择框,找到了文件名输入框并将要上传的文件的路径填入该输入框,然后模拟按下回车键,选择文件并点击“确定”按钮完成文件的选择。请注意,要将C:\path\to\your\file.jpg替换为实际要上传的文件的路径。


上传完成后,您可以使用Selenium的等待功能等待页面中上传成功的提示信息出现,然后关闭浏览器

新实例:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from pywinauto.application import Application
from pywinauto.keyboard import send_keys
import time
# 指定要上传的文件路径
file_path = r"C:\Users\Administrator\Desktop\1\f69e1dd88b3e90726e4d6cee1baa709.jpg"
# 创建WebDriver对象,这里使用Chrome浏览器作为示例
driver = webdriver.Chrome()
# 打开要上传文件的页面
driver.get("https://layuion.com/docs/modules/upload.html")
# 等待上传按钮出现并点击
upload_button = WebDriverWait(driver, 10).until(
    EC.element_to_be_clickable((By.ID, "test1"))
)
upload_button.click()
time.sleep(3)
# 使用pywinauto模块模拟系统的文件选择框
app = Application().connect(path="C:\Program Files\Google\Chrome\Application\chrome.exe")  # 打开系统的文件选择框
send_keys(file_path)# 将要上传的文件路径填入文件名输入框
time.sleep(3)
send_keys("{ENTER}")  # 模拟按下回车键,选择文件并点击“确定”按钮
# 等待上传完成
# ...
input('ok?')
# 关闭浏览器
driver.quit()


zhangsir版权a1防采集https://mianka.xyz

扫描二维码推送至手机访问。

版权声明:本文由zhangsir or zhangmaam发布,如需转载请注明出处。

本文链接:https://www.mianka.xyz/post/144.html

分享给朋友:

“用python写个selenium上传文件的程序,非input标签的上传” 的相关文章

在Linux操作系统上使用selenium库

安装selenium模块命令:pip3 install selenium安装chrome命令:yum install https://dl.google.com/linux/direct/google-chrome-stable_current_x86_64....

python 写入文件

一、读写txt文件1、打开txt文件Note=open('x.txt',mode='w',encoding='utf-8')函数=open(x.扩展名,mode=模式)模式种类:w      ...

使用pyautogui进行屏幕捕捉实现自动化操作

import pyautogui import time # # 获取基本信息 # # 屏幕大小 # size = pyautogui.size() # print(size) #&nbs...

权大师商标查询api

''' data:2022-10-15 autor:zhangsir 权大师商标查询api ''' import requests import json import time import h...

python selenium find_element_by_xpath 方法已经被弃用的解决办法

背景:在使用最新3.10.4Python版本时候,用selenium进行xpath定位元素,编译器提示:DeprecationWarning:find_element_by_xpath is deprecated. Please use find_element(by=By.XPATH, value...

python 使用PIL库进行验证码清晰处理

python 使用PIL库进行验证码清晰处理from PIL import Image import sys import os sys.setrecursionlimit(1000000) pixel_list = []...