首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >登录网站

登录网站
EN

Stack Overflow用户
提问于 2014-09-25 02:51:09
回答 1查看 56关注 0票数 0

我正在尝试运行一个登录到几个网站的VB脚本。我可以让列表中的第一个网站登录,但当它打开一个新的标签并尝试下一个网站时,我遇到了问题。我已经验证了it和值都是正确的,并且网页在尝试加载之前已经加载。它似乎无法在第二个选项卡上看到表单之类的内容。任何帮助都将不胜感激!

代码语言:javascript
运行
复制
'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
EN

回答 1

Stack Overflow用户

发布于 2014-09-25 04:57:31

Navigate2创建了一个新的IE对象,你必须找到它。

您的IE对象只是对第一个选项卡的引用。Navigate2方法创建一个未被IE对象引用的新窗口。可以从shell.application对象访问新窗口,但您必须标识要操作的正确IE或窗口。

代码语言:javascript
运行
复制
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()循环中的比较条件。

代码语言:javascript
运行
复制
if instr(1, w.document.title, sTitle, vbTextCompare) = 1 then

w是窗口,因此:

  • 使用Window.LocationURL属性:
    • ``if instr(1,w.LocationURL,sURL,vbTextCompare) =1
    • 您必须定义sURL instr

  • 使用Window.Document.URL属性:
    • ``if instr(1,w.Document.URL,sURL,vbTextCompare) =1
    • 您必须定义sURL instr

此外,一旦有了窗口引用w,就可以对文档执行任何其他操作,如w.Document.Focus

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26024301

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档