0
首先。我是編程的傀儡。VBA/javascript。腳本使瀏覽器動作自動化
我試圖在Excel中使用Javascript和VBA來自動執行一些IE操作來轉到某個頁面並從該頁面下載Excel文件,因爲我需要每天都這樣做。
a)....要下載的excel文件沒有鏈接。 b)VBA腳本在自動登錄時表現良好,但當IE進入第二頁(不同的URL)時,它總是彈出運行時錯誤70-權限被拒絕。我試圖修改IE選項中的安全性,但不工作。
c)....我使用JavaScript來做自動登錄,它也運行得很好,但IE瀏覽器進入差異頁面後,似乎停止工作。
d)....所以我懷疑它可能是我的腳本沒有抓住新的HTML文檔,當URL更改。
請幫忙。代碼連接
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 bcuz it's js webpage? i dont 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的
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()}
}