我正在尝试运行一个登录到几个网站的VB脚本。我可以让列表中的第一个网站登录,但当它打开一个新的标签并尝试下一个网站时,我遇到了问题。我已经验证了it和值都是正确的,并且网页在尝试加载之前已经加载。它似乎无法在第二个选项卡上看到表单之类的内容。任何帮助都将不胜感激!
'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发布于 2014-09-25 04:57:31
Navigate2创建了一个新的IE对象,你必须找到它。
您的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 thenw是窗口,因此:
此外,一旦有了窗口引用w,就可以对文档执行任何其他操作,如w.Document.Focus。
https://stackoverflow.com/questions/26024301
复制相似问题