# *************************************************上传文件操作*************************************************
import win32gui
import win32con
# 谷歌浏览器
def upload_file(file_path):
# 一级窗口
dialog = win32gui.FindWindow("#32770", "打开")
# 二级窗口
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", "打开(&O)")
# 输入文件地址:
win32gui.SendMessage(edit, win32con.WM_SETTEXT, None, file_path)
# 点击打开按钮,提交文件
win32gui.SendMessage(dialog, win32con.WM_COMMAND, 1, button)
upload_file(r"D:\python\sl.txt")
"""
autoit、pywin32
http://testingpai.com/article/1595507248769
# 1、pywinauto => pip install pywinauto
# 缺点:只能在windows上使用
# 优点:可以选择多个文件,路径中有中文也能可以
# 2、pyauogui => pip install pyautogui
# 优点: 跨平台(linux,mac,windows都可以用)
# 缺点:只能选择一个文件,文件路径有中文会出问题
# ------------pywinauto 通过窗口上传文件----------------------
from pywinauto.keyboard import send_keys
输入文件名
# send_keys('C:\课件\images\9.png')
# # 输入回车键
# send_keys('{VK_RETURN}')
------------pyautogui 通过窗口上传文件----------------------
# 输入文件名
pyautogui.write('D:\selenium-screenshot-4.png')
# 输入回车键,注意要按2次
pyautogui.press('enter', 2)
"""
from selenium import webdriver
import pyautogui
from time import sleep
from selenium.webdriver.common.action_chains import ActionChains
from pywinauto.keyboard import send_keys
driver = webdriver.Chrome()
driver.maximize_window()
driver.get("https://www.ketangpai.com/User/login.html")
sleep(2)
driver.find_element("xpath", '//input[@name="account"]').send_keys("2501768591@qq.com")
driver.find_element("xpath", '//input[@name="pass"]').send_keys("yuan5311645")
driver.find_element('xpath', '//div[@id="login"]//div[contains(@class,"pt-login")]//a[text()="登录"]').click()
sleep(7)
driver.find_element('xpath', '//a[@title="Python全栈第33期"]').click()
sleep(6)
driver.find_element('xpath', '//div[@id="third-nav"]//a[text()="资料"]').click()
sleep(5)
driver.find_element('xpath', "//div[contains(@class,'add-resource-btn')]").click()
sleep(5)
# 点击 上传本地文件 按钮,弹出windows对话框
ele = driver.find_element('xpath', '//a[contains(@class,"webuploader-container")]')
ActionChains(driver).click(ele).perform()
# 等待windows弹框出现
sleep(4)
# pyautogui的用法
# pyautogui.write('D:\\BaiduNetdiskDownload\\report_.html')
# pyautogui.press('enter', presses=2)
# pywinauto的用法
send_keys(r'C:\picture1.jpg')
# send_keys('"D:\selenium-screenshot-4.png"')
# 输入回车键
send_keys('{VK_RETURN}')
sleep(10)
# driver.quit()
python自动化测试--文件上传
于 2019-12-29 09:41:16 首次发布