selenium通过pywin32库上传文件

安装pywin32库

pip install pypiwin32

代码

以网站https://tinypng.com/为例,下面直接附上代码:

 1 from selenium import webdriver
 2 import win32com.client
 3 import time
 4 #获取shell对象
 5 shell = win32com.client.Dispatch("WScript.Shell")
 6 d = webdriver.Chrome()
 7 d.maximize_window()
 8 d.get("https://tinypng.com/")
 9 d.find_element_by_css_selector("section.upload").click()
10 time.sleep(2)
11 #使用shell对象的Sendkeys方法给应用程序发送字符串
12 shell.Sendkeys(r"C:\Users\Administrator\Pictures\123.png"+'{ENTER}'+'\n')
13 d.implicitly_wait(2)
14 txt = d.find_element_by_css_selector("li.upload>div").text
15 #验证是否上传成功
16 if "123.jpg" in txt:
17     print("test pass")
18 else:
19     print("test fail")

 

posted @ 2020-04-18 17:36  与夜色融为一体  阅读(450)  评论(0编辑  收藏  举报