使用Excel VBA和InternetExplorer提交表单并阅读结果 [英] Submitting form and reading results using Excel VBA and InternetExplorer

查看:97
本文介绍了使用Excel VBA和InternetExplorer提交表单并阅读结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用InternetExplorer对象时使用Excel VBA提交表单.提交后,我可以在屏幕上看到URL更改.但是,当我尝试输出URL(以确认它已更改并且代码知道它)时,我得到了相同的URL.

I'm submitting a form using Excel VBA while using an InternetExplorer object. Once submitted, I can see the URL change on screen. However, when I attempt to output the URL (to confirm that it changed and the code knows it), I get the same URL.

在下面的两个调试语句中,它们输出相同的URL.

In both debug statements below, they output the same URL.

代码:

Dim username As String
Dim password As String
Dim server_ip As String

username = "aaa"
password = "bbb"
server_ip = "ip_here"

Dim ie As New InternetExplorer

Dim doc As HTMLDocument
Set doc = New MSHTML.HTMLDocument

Dim url As String

ie.Visible = True
ie.navigate "my_url"

'wait
Do
DoEvents
Loop Until ie.readyState = READYSTATE_COMPLETE
Set doc = ie.document
Debug.Print "url: " & doc.url ' is /abc.html

'set credentials
doc.all.username.Value = username
doc.all.password.Value = password

'submit
ie.document.getElementsByTagName("form")(0).submit

Debug.Print "submitted..."

'wait
Do
DoEvents
Loop Until ie.readyState = READYSTATE_COMPLETE

Set doc = ie.document
Debug.Print "url: " & doc.url 'should be /def.html, but returns /abc.html

推荐答案

readystate_complete的查询仅以这种方式工作一次.之后,状态保持不变.因此,如有必要,您可以手动暂停.

The query on readystate_complete works only once in this way. After that the status remains the same. Therefore you can work with a manual pause if necessary.

'The last three values are hours, minutes, seconds
'This waits for 5 seconds
Application.Wait (Now + TimeSerial(0, 0, 5))

另一种方法是等待循环,直到找到已知的html元素.查看此示例以获取更多信息:
在线搜索栏值从excel导出后,未单击自动标记即可识别错误

Another way is to wait with a loop until a known html element is found. Look at this example for more information:
Online search bar values after export from excel not clicking automatically tag identify wrong

使用循环的另一个示例:
Excel VBA-网页抓取-内部文本HTML表格单元格

One more example for using a loop:
Excel VBA - Web Scraping - Inner Text of HTML Table Cell

这篇关于使用Excel VBA和InternetExplorer提交表单并阅读结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆