我有一个简短的excel宏,旨在: 1)打开Internet Explorer并导航到“http://www.puco.ohio.gov/pucogis/address/search.cfm” 2)使用excel工作簿中的数据填写该站点上的表单 3)单击按钮以提交表单 4)从网站上抓取一些内部文本并将其放在工作簿中的单元格中 5)关闭Internet Explorer
我无法让第3步工作。也就是说,我无法使用点击/提交功能来使用本网站。单击该按钮时,网站会填充特定于表单中输入信息的信息。代码中的其他所有内容都在工作。我已经搜索了一个答案并尝试了提交经文点击方法而没有运气。
谢谢你的帮助。
以下代码:
Private Sub SiteData()
Dim ie As Object
Dim utility As Variant
Dim HTMLButton
Set ie = CreateObject("InternetExplorer.Application")
ie.Navigate "http://www.puco.ohio.gov/pucogis/address/search.cfm"
ie.Visible = True
While ie.Busy
DoEvents
Wend
ie.Document.all("address").Value = ThisWorkbook.Sheets("Site Info").Range("D14")
While ie.Busy
DoEvents
Wend
Set HTMLButton = ie.Document.getElementsByTagName("input")(1)
HTMLButton.Click
While ie.Busy
DoEvents
Wend
Set utility = ie.Document.getElementById("supName")
ThisWorkbook.Sheets("Site Info").Range("D50") = utility.innerText
ie.Quit
Set ie = Nothing
End Sub
答案 0 :(得分:0)
尝试使用此解决方案,我从this answer找到similar question。该答案未被接受,但我已使用您的代码对此进行了测试,并且似乎正在运行。
Private Sub SiteData()
Dim ie As Object
Dim utility As Variant
Dim HTMLButton
Set ie = CreateObject("InternetExplorer.Application")
ie.Navigate "http://www.puco.ohio.gov/pucogis/address/search.cfm"
ie.Visible = True
While ie.Busy
DoEvents
Wend
ie.Document.all("address").Value = ThisWorkbook.Sheets("Site Info").Range("D14")
While ie.Busy
DoEvents
Wend
Call ie.Document.parentWindow.execScript("codeAddress()")
While ie.Busy
DoEvents
Wend
Set utility = ie.Document.getElementById("supName")
ThisWorkbook.Sheets("Site Info").Range("D50") = utility.innerText
ie.Quit
Set ie = Nothing
End Sub
如果您不知道或无法合理预期函数调用codeAddress()
,那么您可以尝试这样的操作从按钮onclick
中获取它属性:
Dim fn$
fn = HTMLButton.onclick
fn = Mid(fn, InStr(fn, "{"))
fn = Trim(Replace(Replace(Replace(fn, "{", vbNullString), "}", vbNullString), vbLf, vbNullString))
Call ie.Document.parentWindow.execScript(fn)
答案 1 :(得分:0)
您可以直接调用JavaScript。试试这个会起作用
代替:
设置HTMLButton = ie.Document.getElementsByTagName(“input”)(2)
HTMLButton.Click
使用
ie.Document.parentWindow.execScript code:=“codeAddress()”
Private Sub CommandButton1_Click()
Dim ie As Object
Dim utility As Variant
Dim HTMLButton
Set ie = CreateObject("InternetExplorer.Application")
ie.Navigate "http://www.puco.ohio.gov/pucogis/address/search.cfm"
ie.Visible = True
While ie.Busy
DoEvents
Wend
ie.Document.all("address").Value = ThisWorkbook.Sheets("Site Info").Range("D14")
While ie.Busy
DoEvents
Wend
ie.Document.parentWindow.execScript code:="codeAddress()"
'Set HTMLButton = ie.Document.getElementsByTagName("input")(2)
'HTMLButton.Click
While ie.Busy
DoEvents
Wend
Set utility = ie.Document.getElementById("supName")
ThisWorkbook.Sheets("Site Info").Range("D16") = utility.innerText
ie.Quit
Set ie = Nothing
End Sub
还要感谢这篇文章帮助我解决了你的问题 How to find and call javascript method from vba