selenium (七) 上传文件

由于上传时,需要在window系统上有选择文件的操作,因此需要
借助一个工具:AutoIt,下载地址:https://www.autoitscript.com/files/autoit3/autoit-v3-setup.exe ;下载后直接安装即可。

第一步:打开Auto Window Info 工具,获取window上控件的信息.

第二步:打开SciTE  Script Editor工具编写脚本。

;ControlFocus ( "title", "text", controlID )
;title:要访问的窗口的标题/ hWnd /类
;text:要访问的窗口的文本。
;controlID:控件ID
ControlFocus("打开","","Edit1")
;WinWait ( "title" [, "text" [, timeout = 0]] )
;暂停脚本的执行,直到请求的窗口存在。
;title:要检查的窗口的标题/ hWnd /类。
;text:[可选]要检查的窗口的文本。 默认值为空字符串
;[可选]如果窗口不存在,则以秒为单位超时。 默认值为0(无超时)。
WinWait("[CLASS:#32770]","",10)
;ControlSetText ( "title", "text", controlID, "new text" [, flag = 0] )
;设置控件的文本。
;title:要访问的窗口的标题/ hWnd /类。
;text:要访问的窗口的文本
;controlID:控件ID
;new text:要设置到控件中的新文本。
;flag:[可选]当与0(默认值)不同时将强制重绘目标窗口。
ControlSetText("打开","","Edit1","C:\Users\Administrator\Desktop\QQ20190503193548.png")
;ControlClick ( "title", "text", controlID [, button = "left" [, clicks = 1 [, x [, y]]]] )
;将鼠标单击命令发送到给定控件。
;title:要访问的窗口的标题/ hWnd /类。
;text:要访问的窗口的文本
;controlID:控件ID
;button:[可选]单击,“左”,“右”,“中”,“主”,“菜单”,“主要”,“次要”按钮。 默认为左按钮。
;clicks:[可选]单击鼠标的次数。 默认值为1。
;x:[可选]在控件中单击的x位置。 默认为中心。
;y:[可选]在控件中单击的y位置。 默认为中心。
ControlClick("打开","","Button1")

第三步,打开Compile Script to .exe 工具,对脚本进行编译.

第四步,使用Python执行编译过的Auto 脚本。

#-*-encoding:utf-8-*-
from selenium import webdriver
from time import sleep
import os


driver=webdriver.Chrome()
driver.get("http://testshop:8081/tshop/index.php?con=simple&act=login")


def login():
	element_username=driver.find_element_by_xpath('//ul[@class="form  "]/li/input[@name="account"]')
	element_username.send_keys("[email protected]")
	element_password=driver.find_element_by_xpath('//ul[@class="form  "]/li[2]/input[@name="password"]')
	element_password.send_keys("123456")
	element_button=driver.find_element_by_xpath('//ul[@class="form  "]/li[4]/button')
	element_button.click()
	sleep(1)

def click_myOrder_link():
	sleep(1)
	element_myOrder=driver.find_element_by_link_text(u"我的订单")
	element_myOrder.click()


def click_myInfo():
	sleep(1)
	element_myInfo=driver.find_element_by_link_text(u"个人资料")
	element_myInfo.click()


def change_picture():
	sleep(1)
	element_changeLink=driver.find_element_by_link_text(u"修改头像")
	element_changeLink.click()
	element_chooseBtn=driver.find_element_by_xpath('//form[@enctype="multipart/form-data"]/p/input[@name="imgFile"]')
	element_chooseBtn.click()
	sleep(1.5)
	status=os.system("C:\\Users\\Administrator\\Desktop\\test.exe")
	print status
	sleep(3)
	element_uploadBtn=driver.find_element_by_xpath('//form[@enctype="multipart/form-data"]/p[2]/button')
	element_uploadBtn.click()



if __name__=="__main__":
	login()
	click_myOrder_link()
	click_myInfo()
	change_picture()

  以上代码可以完成,个人中心的修改头像功能

 

猜你喜欢

转载自www.cnblogs.com/JcHome/p/10808010.html
今日推荐