'''百度首頁搜索上傳圖片為例'''
#前提條件 1.使用winspy定位絕對路徑
#前提條件 2.安裝庫pywin32 采用pip安裝pip install pywin32 或其他途徑安裝
#1.訪問百度網址
#2.點擊圖片按鈕

#3.點擊上傳

#4.使用winspy工具定位路徑輸入框 和打開按鈕 如何定位請自行查找 參考https://www.cnblogs.com/xiaogongjin/p/11546518.html#4361591

#5.輸入絕對路徑,點擊打開 ,前提條件為,windows 上傳彈框已經可見,可以sleep1-2秒等待出現
小白環境 Name: Version:
python 3.7
WinSpy 1.0.2.7
pywin32 227
鏈接:https://pan.baidu.com/s/1fgIVbyPXbVenRMxSb5u89Q
提取碼:7wuo
注意:pywin32兼容Python版本,以上環境親測可用
pywin32各版本連接:https://github.com/mhammond/pywin32/releases
下方代碼可直接復制執行,需要更新上傳文件路徑
from selenium import webdriver
import win32gui
import win32con
import time
driver=webdriver.Chrome()
driver.get('https://www.baidu.com')
driver.maximize_window()#最大
driver.find_element_by_class_name('soutu-btn').click()
time.sleep(2)
imgpath='D:\\abc.jpg'
driver.find_element_by_class_name('upload-pic').click()
time.sleep(2)
# driver.find_element_by_class_name('upload-pic').send_keys('imgpath')
#一級頂層窗口,此處title為上傳窗口名稱,瀏覽器不一樣上傳窗口名稱不一樣
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',None)
#執行操作 輸入文件路徑
win32gui.SendMessage(edit,win32con.WM_SETTEXT,None,'D:\\abc.jpg')
#點擊打開上傳文件
win32gui.SendMessage(dialog,win32con.WM_COMMAND,1,button)
打包封裝方法
def file_upload(self,filePath,browser_type='chrome'):
'''
windows 上傳彈框已經可見,可以sleep1-2秒等待出現
:param filePath: 文件絕對路徑例 D:\\123.abc
:param browser: 瀏覽器類型默認為 chrome
:return:
'''
if browser_type.lower()== 'chrome':
title='打開'
elif browser_type.lower()== 'Firefox':
title='文件上傳'
elif browser_type.lower() =='ie':
title='選擇要加載的文件'
logging.info('本次使用{0}進行上傳操作'.format(browser_type))
# 一級頂層窗口,此處title為上傳窗口名稱,瀏覽器不一樣上傳窗口名稱不一樣
dialog = win32gui.FindWindow("#32770", title)
# 二級窗口
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', None)
# 執行操作 輸入文件路徑
win32gui.SendMessage(edit, win32con.WM_SETTEXT, None, filePath)
# 點擊打開上傳文件
win32gui.SendMessage(dialog, win32con.WM_COMMAND, 1, button)
鏈接:https://pan.baidu.com/s/1fgIVbyPXbVenRMxSb5u89Q
提取碼:7wuo
安裝狀況1:安裝時提示無法找到注冊信息
解決辦法參考鏈接:https://blog.csdn.net/u011702002/article/details/79624256
復制以下代碼,執行后重新安裝
代碼
py3.5.2
import sys
from winreg import *
# tweak as necessary
version = sys.version[:3]
installpath = sys.prefix
regpath = "SOFTWARE\\Python\\Pythoncore\\%s\\" % (version)
installkey = "InstallPath"
pythonkey = "PythonPath"
pythonpath = "%s;%s\\Lib\\;%s\\DLLs\\" % (
installpath, installpath, installpath
)
def RegisterPy():
try:
reg = OpenKey(HKEY_CURRENT_USER, regpath)
except EnvironmentError as e:
try:
reg = CreateKey(HKEY_CURRENT_USER, regpath)
SetValue(reg, installkey, REG_SZ, installpath)
SetValue(reg, pythonkey, REG_SZ, pythonpath)
CloseKey(reg)
except:
print("*** Unable to register!")
return
print("--- Python", version, "is now registered!")
return
if (QueryValue(reg, installkey) == installpath and
QueryValue(reg, pythonkey) == pythonpath):
CloseKey(reg)
print("=== Python", version, "is already registered!")
return
CloseKey(reg)
print("*** Unable to register!")
print("*** You probably have another Python installation!")
if __name__ == "__main__":
RegisterPy()
狀況2:安裝后 import win32gui異常
pip install pypiwin32 安裝后重新正常導入即可