VBA/javascript。自动化浏览器操作的脚本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7564753/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
VBA/javascript. script to automate browser actions
提问by user614244
First of all. I'm a dummy in programming.
首先。我在编程方面是个傻瓜。
I was trying to use JavaScript and VBA in Excel to automate some IE actions to go to some page and download Excel files from that page, as I need to do this everyday.
我试图在 Excel 中使用 JavaScript 和 VBA 来自动执行一些 IE 操作,以转到某个页面并从该页面下载 Excel 文件,因为我每天都需要这样做。
The excel file to be downloaded does not have a link.
The VBA script did well in auto login, but when the IE goes to a second page ( different URL ), it always pops up a run time error 70 -- permission denied. I tried to modified the security in IE options but not working.
I use JavaScript to do the auto login and it worked well too, but again after the IE goes to a diff page, it seems stop working.
So I suspect that it might be that my script did not grab the new html document when url changes.
要下载的excel文件没有链接。
VBA 脚本在自动登录方面做得很好,但是当 IE 转到第二个页面(不同的 URL)时,它总是弹出运行时错误 70 -- 权限被拒绝。我试图修改 IE 选项中的安全性但不起作用。
我使用 JavaScript 进行自动登录,它也运行良好,但是在 IE 转到差异页面后,它似乎停止工作。
所以我怀疑可能是我的脚本在 url 更改时没有抓取新的 html 文档。
VBA:
VBA:
Sub vbadownload()
Dim objIE As SHDocVw.InternetExplorer ' internet controls
Dim htmlDoc As MSHTML.HTMLDocument ' html object lib
Dim htmlInput As MSHTML.HTMLInputElement
Dim htmlDiv As MSHTML.HTMLDivElement
Dim htmlColl As MSHTML.IHTMLElementCollection
Set objIE = New SHDocVw.InternetExplorer
''''''''''' first log in
With objIE
.Navigate "mainpage.com" ' log in page
.Visible = 1
Do While .READYSTATE <> 4: DoEvents: Loop
Application.Wait (Now + TimeValue("0:00:01"))
'set user name and password
Set htmlDoc = .Document
Set htmlColl = htmlDoc.getElementsByTagName("INPUT")
Do While htmlDoc.READYSTATE <> "complete": DoEvents: Loop
For Each htmlInput In htmlColl
If htmlInput.Name = "username" Then
htmlInput.Value = "xxxx" 'username
Else
If htmlInput.Name = "password" Then
htmlInput.Value = "xxxx" 'password
End If
End If
Next htmlInput
'click login
Set htmlDoc = .Document
Set htmlColl = htmlDoc.getElementsByTagName("input")
Do While htmlDoc.READYSTATE <> "complete": DoEvents: Loop
For Each htmlInput In htmlColl
If Trim(htmlInput.Type) = "submit" Then
htmlInput.Click 'click
Exit For
End If
Next htmlInput
' now it goes to second page after submit
' and I want click some button on second page, it says permission denied.
' so how do i make below codes work on current page without typing new url. ( as the url actually does not change, I guess because it's js webpage? I don't know)
Set htmlDoc = .Document
Set htmlColl = htmlDoc.getElementsByTagName("div")
Do While htmlDoc.READYSTATE <> "complete": DoEvents: Loop
For Each htmlDiv In htmlColl
If Trim(htmlDiv.ID) = "123" Then
htmlDiv.Click 'click
Exit For
End If
Next htmlDiv
End With
JavaScript
JavaScript
var ie = new ActiveXObject("InternetExplorer.Application");
ie.visible=true;
ie.navigate("www.sample.com");
while(ie.busy)(WScript.sleep(100));
var doc1 = ie.document;
var window1 = doc1.window;
var form1 = doc1.forms[0];
form1.username.value="xxx";
form1.password.value="xxxx";
form1.submit();
/* again, from here, it goes to second page, and it stops working.
* how do I get/parse current html document? */
var div1 = doc1.getElementsByTagName("div");
for (var i=0; i<div1.length; ++i)
{
if (div1[i].className="fff")
{div1[i].click()}
}
回答by Tim Williams
Every time you refresh the page (by submitting a form or using .navigate) you need to repeat your "sleep" loop so that you wait for the page to complete loading, and then grab a new reference to the document object. After you navigate it's a new page, so you can't just continue to use the "old" document reference.
每次刷新页面(通过提交表单或使用 .navigate)时,您都需要重复“睡眠”循环,以便等待页面完成加载,然后获取对文档对象的新引用。导航后它是一个新页面,因此您不能继续使用“旧”文档参考。