Internet explorer 登录网站

Internet explorer 登录网站,internet-explorer,vbscript,Internet Explorer,Vbscript,我试图运行一个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 e

我试图运行一个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
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对象。因此,您可以根据中可用的任何方法重新定义getWindow函数来标识窗口

更改主getWindow循环中的比较条件。 w是窗口,因此:

使用属性: `如果instr1,w.LocationURL,sURL,vbTextCompare=1,则 您必须定义sURL变量。 使用属性: `如果instr1,w.Document.URL,sURL,vbTextCompare=1,则 您必须定义sURL变量。
此外,一旦有了窗口引用w,您就可以对文档执行任何其他操作,如w.document.Focus。

谢谢您的帮助。我怎么知道新标签的标题是什么?这是一个comcast页面,因此选项卡上的宣传语本身就是登录comcast,但这不起作用。有没有办法建立一个打开选项卡的列表,并找到在新选项卡打开之前不存在的选项卡?@Jeff您必须像在getWindow函数中一样循环,并将标题或引用存储到单独的数组中。检查每个窗口是否存在于该数组中。有什么新的发展吗?我没办法弄清楚。我结束了一项工作,即一旦到达目的地,我将地址复制到一个新选项卡中,然后在原始选项卡中开始下一个选项卡。它也有用!谢谢你的帮助。
if instr(1, w.document.title, sTitle, vbTextCompare) = 1 then