uibot自动登陆163邮箱发送邮件

本文涉及uibot技术:

  1. css-selector
  2. js函数调用
  3. excel文件写入和读取
  4. 鼠标和键盘
Dim arrRet = ""
Dim arrElement = ""
Dim objExcelWorkBook = ""
Dim objDatatable = ""
Dim hWeb = ""
Dim bRet = ""
Dim arr=[]
Dim body = []

For i = 1 To 3 Step 1 
	push(body,["123@163.com","UiBot初级实践题"&i,"RPA实施工程师(初级)实践题"])
Next
TracePrint(body)
/*
    uibot自动登陆163邮箱,由于页面中的很多id属性都是动态的,换句话说,他只要刷新页面,它的id值就会变化,因此绝不能简单的通过uibot提供的鼠标点击
    等简单实现,需要通过分析页面中固定不变的信息,从而达到确认唯一的目标
    css-selector的强大功能为我们提供了方便

*/
// 构建数据表,表头和数据
objDatatable = Datatable.BuildDataTable(body,["收件人","邮件主题","邮件内容"])
// 将数据表转化为数组
objDatatable = Datatable.GetDataTableByArray(objDatatable,True)
// 打开excel
objExcelWorkBook = Excel.OpenExcel("C:\\Users\\Administrator\\Desktop\\初级uibot考试.xlsx",True,"Excel","","")
// 将数组中的数据写入excel表
Excel.WriteRange(objExcelWorkBook,"Sheet1","A1",objDatatable,False)
// 打开163网站
hWeb = WebBrowser.Create("chrome","https://mail.163.com/",30000,{"bContinueOnError":False,"iDelayAfter":300,"iDelayBefore":200,"sBrowserPath":"","sStartArgs":""})
#icon("@res:o69kapd9-hvfb-5h75-8n0i-rimnaefc0l56.png")
// 获取输入框的特征固定祖宗元素,然后获取子元素
arrElement1 = UiElement.GetChildren({"wnd":[{"cls":"Chrome_WidgetWin_1","title":"*","app":"chrome"},{"cls":"Chrome_RenderWidgetHostHWND","title":"Chrome Legacy Window"}],"html":[{"id": "loginDiv","tag": "DIV"}],"wnd": [{"app": "chrome","cls": "Chrome_WidgetWin_1","title": "*"},{"cls": "Chrome_RenderWidgetHostHWND","title": "Chrome Legacy Window"}]},{"bContinueOnError":False,"iDelayAfter":300,"iDelayBefore":200})

#icon("@res:c82qul3f-2b7d-69mb-km6p-aibh91vt36op.png")
// 通过页面分析,手动组装html特征
arrRet = push(arr,arrElement1[0]["html"][0])
arrRet = push(arrRet,{"id":"account-box","tag":"DIV"})
TracePrint(arrRet)
#icon("@res:cidad63e-muct-q1ms-vms1-jl9ntv4in9vi.png")
// 获取arrRet的子元素
arrElement = UiElement.GetChildren({"wnd":[{"cls":"Chrome_WidgetWin_1","title":"*","app":"chrome"},{"cls":"Chrome_RenderWidgetHostHWND","title":"Chrome Legacy Window"}],"html":arrRet},{"bContinueOnError":False,"iDelayAfter":300,"iDelayBefore":200})
TracePrint(arrElement)
TracePrint(arrElement1[0]["html"][0])
TracePrint(arrElement[2]["html"][1])
#icon("@res:fs31osur-2i6q-vl29-tmd3-2ts1i3d5qhek.png")
// 用户名输入
Keyboard.InputText({"wnd":[{"cls":"Chrome_WidgetWin_1","title":"*","app":"chrome"},{"cls":"Chrome_RenderWidgetHostHWND","title":"Chrome Legacy Window"}],"html":[arrElement1[0]["html"][0],{"css-selector":"body>div>div>div>form>div>div>div","idx":1,"parentid":"account-box","tag":"DIV"}]},"123",True,20,10000,{"bContinueOnError":False,"iDelayAfter":300,"iDelayBefore":500,"bSetForeground":True,"sSimulate":"message","bValidate":False,"bClickBeforeInput":False})
#icon("@res:afhslqjc-7juu-qg1g-t46o-ri49h6m76hcb.png")
arrElement3 = UiElement.GetChildren({"wnd":[{"cls":"Chrome_WidgetWin_1","title":"*","app":"chrome"},{"cls":"Chrome_RenderWidgetHostHWND","title":"Chrome Legacy Window"}],"html":[arrElement1[0]["html"][0],{"css-selector":"div>div>div[class='u-input box']","parentid":"login-form","tag":"DIV"}]},{"bContinueOnError":False,"iDelayAfter":300,"iDelayBefore":200})
#icon("@res:qhsqv18q-e5rp-rcpm-eoh9-mporer1g4206.png")
// 
TracePrint(arrElement3[2]["html"])
// 密码输入
#icon("@res:763rspva-875m-e7t4-cm7j-g6l0kpen2p4h.png")
Keyboard.InputText({"wnd":[{"cls":"Chrome_WidgetWin_1","title":"*","app":"chrome"},{"cls":"Chrome_RenderWidgetHostHWND","title":"Chrome Legacy Window"}],"html":arrElement3[2]["html"]},"123",True,20,10000,{"bContinueOnError":False,"iDelayAfter":300,"iDelayBefore":500,"bSetForeground":True,"sSimulate":"message","bValidate":False,"bClickBeforeInput":False})
#icon("@res:n8uv090a-cuua-2lbg-18qr-iig9r849hbti.png")
// 点击登陆
Mouse.Action({"wnd":[{"cls":"Chrome_WidgetWin_1","title":"*","app":"chrome"},{"cls":"Chrome_RenderWidgetHostHWND","title":"Chrome Legacy Window"}],"html":[arrElement1[0]["html"][0],{"tag":"A","id":"dologin"}]},"left","click",10000,{"bContinueOnError":False,"iDelayAfter":300,"iDelayBefore":200,"bSetForeground":True,"sCursorPosition":"Center","iCursorOffsetX":0,"iCursorOffsetY":0,"sKeyModifiers":[],"sSimulate":"simulate","bMoveSmoothly":False})
#icon("@res:dfpkj8nj-70ff-t864-7cog-73of2d2gqgi7.png")
#icon("@res:2tqi8a07-rbbp-svkb-ddsp-p7vhg7p3dgi8.png")
arrElement = UiElement.GetChildren({"wnd":[{"cls":"Chrome_WidgetWin_1","title":"*","app":"chrome"},{"cls":"Chrome_RenderWidgetHostHWND","title":"Chrome Legacy Window"}],"html":[{"tag":"LI","css-selector":"div#dvNavTop>ul>li:nth-child(2)"}]},{"bContinueOnError":False,"iDelayAfter":300,"iDelayBefore":200})
TracePrint(arrElement[0]["html"])
#icon("@res:1s6kin0v-4mqh-lv0i-r2un-ia4ghdpntiq8.png")
//点击写信
Mouse.Action({"wnd":[{"cls":"Chrome_WidgetWin_1","title":"*","app":"chrome"},{"cls":"Chrome_RenderWidgetHostHWND","title":"Chrome Legacy Window"}],"html":arrElement[0]["html"]},"left","click",10000,{"bContinueOnError":False,"iDelayAfter":300,"iDelayBefore":200,"bSetForeground":True,"sCursorPosition":"Center","iCursorOffsetX":0,"iCursorOffsetY":0,"sKeyModifiers":[],"sSimulate":"simulate","bMoveSmoothly":False})

// 遍历列
For i = 2 To Excel.GetRowsCount(objExcelWorkBook,"Sheet1") Step 1 
    // 遍历行
	For j = Asc("A") To Excel.GetColumsCount(objExcelWorkBook,"Sheet1")+Asc("A")-1 Step 1 
        // 读取每一个单元格
		info = Excel.ReadCell(objExcelWorkBook,"Sheet1",Chr(j)&i)	
		#icon("@res:mqns2b8n-m159-m622-g63c-60pt074n4avs.png")
        // 判断是否为收件人
		If j = Asc("A")
			#icon("@res:3ij5ofl7-5tli-r0sm-eger-9tutkr9lg6i4.png")
			arrElement = UiElement.GetChildren({"wnd":[{"cls":"Chrome_WidgetWin_1","title":"*","app":"chrome"},{"cls":"Chrome_RenderWidgetHostHWND","title":"Chrome Legacy Window"}],"html":[{"tag":"DIV","css-selector":"div.bz0"}]},{"bContinueOnError":False,"iDelayAfter":300,"iDelayBefore":200})
			TracePrint(arrElement[0]["html"])
			TracePrint(arrElement[1]["html"])
			#icon("@res:ul7n1caa-2pl9-casu-h2cd-2rrm19cfviiq.png")
			Keyboard.InputText({"wnd":[{"cls":"Chrome_WidgetWin_1","title":"*","app":"chrome"},{"cls":"Chrome_RenderWidgetHostHWND","title":"Chrome Legacy Window"}],"html":[{"tag":"DIV","css-selector":"div[title=发给多人时地址请以分号隔开]"}]},info,True,20,10000,{"bContinueOnError":False,"iDelayAfter":300,"iDelayBefore":500,"bSetForeground":True,"sSimulate":"message","bValidate":False,"bClickBeforeInput":False})
			TracePrint(arrElement[1]["html"])
			#icon("@res:v625ofet-1jk6-5i45-crnh-74qa7afh5u7s.png")
		Else
            // 是否为邮件主题
			If j = Asc("B")
				arrElement = UiElement.GetChildren({"wnd":[{"cls":"Chrome_WidgetWin_1","title":"*","app":"chrome"},{"cls":"Chrome_RenderWidgetHostHWND","title":"Chrome Legacy Window"}],"html":[{"tag":"DIV","css-selector":"div.dG0>div.bz0>div.js-component-input.nui-ipt"}]},{"bContinueOnError":False,"iDelayAfter":300,"iDelayBefore":200})
				TracePrint(arrElement[0]["html"])
				#icon("@res:a0ofig9b-rgj6-5daf-6fpm-hng0mqfomomq.png")
				Keyboard.InputText({"wnd":[{"cls":"Chrome_WidgetWin_1","title":"*","app":"chrome"},{"cls":"Chrome_RenderWidgetHostHWND","title":"Chrome Legacy Window"}],"html":arrElement[0]["html"]},info,True,20,10000,{"bContinueOnError":False,"iDelayAfter":300,"iDelayBefore":500,"bSetForeground":True,"sSimulate":"message","bValidate":False,"bClickBeforeInput":False})
			Else
                // 邮件内容
                // 由于邮件内容的元素区域涉及跨域问题,所以通过js来抓取域中的元素
				js="
                function(){
                // 将信件内容写入邮件内容区域
                document.querySelector('iframe.APP-editor-iframe').contentWindow.document.querySelector('body.nui-scroll').innerHTML='"&info&"'
                return 123
                }"
                // 绑定当前浏览器
				hWeb = WebBrowser.BindBrowser("chrome",10000,{"bContinueOnError":False,"iDelayAfter":300,"iDelayBefore":200})
                // 运行js代码
				sRet = WebBrowser.RunJS(hWeb,js,True,{"bContinueOnError":False,"iDelayAfter":300,"iDelayBefore":200})
				TracePrint(b)
			End If   
			#icon("@res:iuf51jis-afsm-pap3-0n4h-k8uhg84cm3t3.png")
			
		End If
		
	Next
    // 点击发送
    Mouse.Action({"wnd":[{"cls":"Chrome_WidgetWin_1","title":"*","app":"chrome"},{"cls":"Chrome_RenderWidgetHostHWND","title":"Chrome Legacy Window"}],"html":[{"tag":"SPAN","css-selector":"footer.jp0>div>span.nui-btn-icon"}]},"left","click",10000,{"bContinueOnError":False,"iDelayAfter":300,"iDelayBefore":200,"bSetForeground":True,"sCursorPosition":"Center","iCursorOffsetX":0,"iCursorOffsetY":0,"sKeyModifiers":[],"sSimulate":"simulate","bMoveSmoothly":False})
	// 刷新页面,跳转到写信页面
    WebBrowser.Refresh(hWeb,{"bContinueOnError":False,"iDelayAfter":300,"iDelayBefore":200})
    // 进行下一封邮件的写和发送操作
Next


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值