自动打开网页,点击登录按钮,到了登录窗口,输入账号密码
(由于狐表.net版本的限制,只能调用IE浏览器,无法控制第三方Chrome浏览器)
<DllImport("user32.dll", EntryPoint := "FindWindow")> _ Public Function FindWindow(lpClassName As String, lpWindowName As String) As Integer End Function <DllImport("user32.dll", EntryPoint := "SetParent")> _ Public Function SetParent(hWndChild As Integer, hWndNewParent As Integer) As Integer End Function Public Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As EnumWindowsProc, ByVal lParam As Integer) As Integer Public Delegate Function EnumWindowsProc(ByVal hwnd As Integer, ByVal lParam As Integer) As Boolean Public Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Integer, ByVal lpString As StringBuilder, ByVal cch As Integer) As Integer <DllImport("user32.dll")> _ Public Function GetWindowTextLength(hWnd As Integer) As Integer End Function Public Function EnumWindowsProcCallBack(ByVal hwnd As Integer, ByVal lParam As Integer) As Boolean Return Functions.Execute("EnumWindowsProcCallBack", hwnd, lparam) End Function
'1关闭所有相关的IE浏览器,免得重复出错 Dim shell As Object = CreateObject("Shell.Application") Dim win As Object For Each win In shell.Windows If InStr(win.FullName, "iexplore.exe") > 0 Then If InStr(win.LocationURL, "etax.zhejiang.chinatax.gov.cn") > 0 Then '这里改成你的业务域名,不需要http win.Quit Exit For End If End If Next '2初始化新的IE浏览器 Dim ie As Object = CreateObject("InternetExplorer.Application") ie.Visible = True '3先登录首页 ie.Navigate("https://etax.zhejiang.chinatax.gov.cn:8443") '这里改成你的业务网址 Sleep(4000) '由于这是个Vue异步加载的页面,要给时间它异步加载(如果网络差,建议延长) '4点击登录按钮,跳转到登录窗口 Dim Spans = ie.Document.GetElementsByTagName("span") For Each Span As Object In Spans If Span.GetAttribute("class").ToString = "loginBtnText" Then Span.Click End If Next Sleep(4000) '等异步加载 '5找到账号密码,并输入 Dim Inputs = ie.Document.GetElementsByTagName("input") For Each Input As Object In Inputs Select Case Input.GetAttribute("placeholder").ToString Case "统一社会信用代码/纳税人识别号" input.Focus() Sleep(300) ' 延迟让focus生效 SendKeys.SendWait("914400007578948436") Case "居民身份证号码/手机号码/用户名" input.Focus() Sleep(300) ' 延迟让focus生效 SendKeys.SendWait("13427100453") Case "个人用户密码" input.Focus() Sleep(300) ' 延迟让focus生效 SendKeys.SendWait("123") End Select Next
重复打开IE浏览器,会导致获取元素异常(例如报错`RPC 服务器不可用。 (异常来自 HRESULT:0x800706BA)`),
所以要先关闭好
'1关闭所有相关的IE浏览器,免得重复出错 Dim shell As Object = CreateObject("Shell.Application") Dim win As Object For Each win In shell.Windows If InStr(win.FullName, "iexplore.exe") > 0 Then If InStr(win.LocationURL, "tpass.zhejiang.chinatax.gov.cn") > 0 Then '这里改成你的业务域名,不需要http win.Quit Exit For End If End If Next
这是个主流的Vue页面,采用异步加载。所以我们要适当等待,等待元素加载出来,而**不能单纯通过以下代码**,判断浏览器就绪,否则容易获取不到元素而报错
Do While ie.busy Or ie.readystate <> 4 Application.DoEvents Loop
利用Dim Inputs = ie.Document.GetElementsByTagName("input")找到某种元素的集合,例如这里找到所有的input标签元素。
然后我们去看下要定位的元素,有什么特征。在浏览器按F12,进入网页元素控制台
找到元素后,可以模拟点击
Dim Spans = ie.Document.GetElementsByTagName("span") For Each Span As Object In Spans If Span.GetAttribute("class").ToString = "loginBtnText" Then Span.Click End If Next
找到元素后,可以模拟键盘输入
Dim Inputs = ie.Document.GetElementsByTagName("input") For Each Input As Object In Inputs Select Case Input.GetAttribute("placeholder").ToString Case "统一社会信用代码/纳税人识别号" input.Focus() Sleep(300) ' 延迟让focus生效 SendKeys.SendWait("914400007578948436") End Select Next
注意:Vue/React 前端动态框架阻止了直接 DOM 注入生效的现象,所以狐表论坛里,以下传统的写法是失效的
'这种是失效的 Dim inputs = ie.Document.GetElementsByTagName("input") For Each input As Object In inputs If input.getAttribute("placeholder") = "个人用户密码" Then input.setAttribute("value", 123) End If Next