我花了几个小时阅读Stack Overflow,但找不到适合我的答案。我正在使用VBA,并使用Microsoft Internet Controls和Microsoft HTML Object Library。
这是我的代码(允许我输入用户名和密码信息,即使没有“值”或“id”信息...只有“名称”信息):
' Incorporating Microsoft Internet Controls AND Microsoft HTML Object Library
' Webpage loaded constant
Const READYSTATE_COMPLETE = 4
' Declare Windows API function for setting active window
Declare Function SetForegroundWindow Lib "user32" _
Alias "SetForegroundWindow" (ByVal Hwnd As Long)As Long
' Declare Internet Explorer object
Dim IE As SHDocVw.InternetExplorer
Sub Main
SetMicrophone 0
' create instance of InternetExplorer
Set IE = New InternetExplorer
' using your newly created instance of Internet Explorer
With IE
SetForegroundWindow IE.HWND
.Visible = True
.Navigate2 "http://www.NakedCapitalism.com/wp-admin"
' Wait until page we are navigating to is loaded
Do While .Busy
Loop
Do
Loop Until .readyState = READYSTATE_COMPLETE
' the username and password values will not exist if already logged in
' so no need to fill in the values and go straight to Next
On Error Resume Next
If Err Then
'Do Nothing
Else
' When the page is fully loaded enter your username and password
' you will need to set these appropriately
End If
Dim inputs As MSHTML.IHTMLElementCollection
Dim iFrames As MSHTML.IHTMLElementCollection
Dim iFrame As MSHTML.HTMLFrameElement
' Get top_window frame and navigate to it then
Set doc = IE.document
Set iFrames = doc.getElementsByName("top_window")
If Not iFrames Is Nothing Then
Set iFrame = iFrames(0)
IE.navigate url & iFrame.src
Set inputs = doc.getElementsByName("log")
If Not inputs Is Nothing Then
inputs(0).value = "MyUserName"
End If
End If
Set IE = Nothing
End With
' Get top_window frame and navigate to it then
Set doc = IE.document
Set iFrames = doc.getElementsByName("top_window")
If Not iFrames Is Nothing Then
Set iFrame = iFrames(0)
IE.navigate url & iFrame.src
Set inputs = doc.getElementsByName("pwd")
If Not inputs Is Nothing Then
inputs(0).value = "MyPassword"
End If
End If
Set IE = Nothing
' Tidy Up
SetMicrophone 1
End Sub
我正在尝试点击具有以下代码的按钮:
<p class="submit">
<input type="submit" name="wp-submit" id="wp-submit" class="button button-primary button-large" value="Log In" />
<input type="hidden" name="redirect_to" value="http://www.nakedcapitalism.com/wp-admin/" />
<input type="hidden" name="testcookie" value="1" />
</p>
帮助!?
答案 0 :(得分:0)
这样的东西可能适合你(VBA代码应该类似):
doc.getElementsByName("wp-submit").item(0).click()
对于按钮&#34;单击&#34;方法见:https://msdn.microsoft.com/en-us/library/aa752279(v=vs.85).aspx
表格&#34;提交&#34;方法见:https://msdn.microsoft.com/en-us/library/aa752432(v=vs.85).aspx
这两个链接也可以为您提供帮助,展示如何使用SHDocVW引用IE文档和元素(这就是您正在使用的内容):
https://msdn.microsoft.com/en-us/library/aa752084(v=vs.85).aspx
https://msdn.microsoft.com/en-us/library/ms535862(v=vs.85).aspx
答案 1 :(得分:0)
这个怎么样?
Sub ClickMe()
Dim IE As InternetExplorer
Dim myZip As Long
Dim siteZip As Object
Dim URL As String
Dim gender As Object
Dim btnGo As Object
Sub Macro_1()
myZip = "12345"
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
URL = "Individual and Family Health Insurance"
IE.navigate (URL)
Do
DoEvents
Loop Until IE.readyState = 4
Set siteZip = IE.document.getElementById("census_zipCode")
siteZip.Value = myZip
Set gender = IE.document.getElementById("census_primary_gender")
gender.Value = "MALE"
Set btnGo = IE.document.forms(0).all("method:submit") ' method:submit is the button's name
btnGo.Click
End Sub