使用VB脚本自动登录网站

时间:2015-11-27 13:05:17

标签: html vba internet-explorer vbscript automation

我正在尝试自动化网站的登录过程。我使用下面的代码

Dim objIE
Dim htmld
Set objIE = CreateObject("InternetExplorer.Application")
objIE.Visible = False
objIE.Navigate "website.com"  //website.com is example not the original
While objIE.Busy
WScript.Sleep 400
Wend
WScript.Sleep 500
objIE.Document.getElementById("login_id").value = "ss"

textbox login_id的html代码是

<TABLE id=logintable cellSpacing=0><TBODY>
<TR>
<TD><IMG src="/nfusion/default/en_US/images/sign_in_flag.gif"></TD>
<TD class=tdlabel>User ID:</TD>
<TD><INPUT id=login_id class=txt name=login_id></TD></TR>
<TR>
<TD></TD>

我收到错误的界面未知。我已经更改了安全性,login_id没有标记,所以我无法使用getElementByTag。

2 个答案:

答案 0 :(得分:1)

尝试以下额外检查:

Dim IE
Set IE = CreateObject("InternetExplorer.Application")
With IE
    .Visible = False
    .Navigate "website.com" ' website.com is example not the original
    Do While .Busy Or Not .readyState = 4: WScript.Sleep 100: Loop
    Do Until .document.readyState = "complete": WScript.Sleep 100: Loop
    Do While TypeName(.document.getElementById("login_id")) = "Null": WScript.Sleep 100: Loop
    .document.getElementById("login_id").Value = "ss"
End With

答案 1 :(得分:0)

我写了一个vbscript来做你所问的,我一直都在使用它。它在IE中打开一个选项卡,然后登录,然后在我们的应用程序中向不同页面打开另外两个选项卡。

On Error Resume Next

ECRecord = "http://vm195/views/welcome.action"
ECJobs = "http://vm195/views/job/jobList.action?query.MaxResults=500&allStates=false%2F&query.ActiveState=true&query.ActiveState=false%2F&query.PendingState=true&query.PendingState=false%2F&query.CompletedState=false%2F&query.FailedState=true&query.FailedState=false%2F&query.CancelledState=true&query.CancelledState=false%2F&query.HoldState=true&query.HoldState=false%2F&query.jobTypes=-1&allFreqs=true&allFreqs=false%2F&query.DailyFrequency=true&query.DailyFrequency=false%2F&query.IntervalFrequency=true&query.IntervalFrequency=false%2F&query.SetDateFrequency=true&query.SetDateFrequency=false%2F&query.SingleFrequency=true&query.SingleFrequency=false%2F&query.patientId=&query.accessionNumber=&query.studyPk=&query.dateRange=-3&query.beginDate=&query.endDate=&Submit=Search&refreshRate=120"
ECConfig = "http://vm195/views/org/organizationTree.action"

Set oIE = CreateObject("InternetExplorer.Application")
oIE.Visible = True
'open a new window
oIE.Navigate2 ECRecord

Do While (oIE.Busy)
   WScript.Sleep 10
 Loop                    


 Set Helem = oIE.document.getElementByID("username")
 Helem.Value = "tsu500" ' change this to yours
 Set Helem = oIE.document.getElementByID("password")
 Helem.Value = "production" ' change this to yours

 oIE.Document.getElementsByName("submit_button").Item(0).Click

 'Set Helem = oIE.document.Forms(1)
 'Helem.Submit


WScript.Sleep 500


Do While (oIE.Busy)
   WScript.Sleep 10
 Loop  


'open url In new tab
oIE.Navigate2 ECJobs, 2048
WScript.Sleep 100
oIE.Navigate2 ECConfig, 2048

Set oIE = Nothing