我正在尝试运行一个登录到多个网站的VB脚本。我可以让列表中的第一个网站登录,但是当它打开一个新标签并尝试下一个网站时遇到问题。我已经验证了ID和值都是正确的,并且网页在尝试之前已加载。它似乎无法在第二个选项卡上看到表单等。任何帮助将不胜感激!
'This is all part of a loop, if it's not the first webpage, it'll open a new tab
if i = 2 then
IE.Navigate wp
else
IE.Navigate2 wp, 2048
Wait IE
end if
'My hack at errorhandling, it has to go through the code at least once
'and THEN fail 5 times before it gives up and quits
errnum = 0
On Error Resume Next
try = 0
do until Err.Number = 0 And try = 1
try = 1
if errnum > 5 then
msgbox "STOPPED"
objExcel.ActiveWorkBook.Close
WScript.Quit
End if
Wait IE
Err.Clear
with IE.Document
.getElementByID(unid).value = unval
.getElementByID(pwid).value = pwval
.getElementByID(but).Click
End With
errnum = errnum + 1
loop
答案 0 :(得分:0)
您的IE对象只是对第一个标签的引用。 Navigate2
方法创建一个新窗口,IE对象不会引用该窗口。可以从shell.application
对象访问新窗口,但您必须确定要操作的正确IE或窗口。
newIE = getWindow("Title of New Tab")
if isObject(newIE) then
With newIE.Document
.getElementByID(unid).value = unval
.getElementByID(pwid).value = pwval
.getElementByID(but).Click
End With
else
WScript.echo "Window not found"
end if
Function getWindow(sTitle)
Dim wins, w, result
set wins = createobject("shell.application").windows
for i = 1 to 10 ' max iterations before fail
for each w in wins
on error resume next
if instr(1, typename(w.document),"html", vbTextCompare) > 0 then
if instr(1, w.document.title, sTitle, vbTextCompare) = 1 then
set result = w
do until w.document.readyState = "complete" : WScript.sleep 50 : loop
exit for
end if
end if
on error goto 0
next
if isObject(result) then exit for
WScript.sleep 500 ' ms to wait for each iteration (tab must load before we can get obj)
next
if not(isObject(result)) then result = false;
getWindow = result;
end Function ' getWindow
在getWindow()
函数中,w
被设置为IE对象。因此,您可以根据InternetExplorer object中提供的任何方法重新定义getWindow()
函数以标识窗口。
getWindow()
循环中的比较条件。if instr(1, w.document.title, sTitle, vbTextCompare) = 1 then
w
是窗口,所以:
此外,一旦您拥有窗口引用w
,您就可以对文档执行其他任何操作,例如w.Document.Focus
。