无需导航到此网页即可访问网页的框架文档 [英] Access to a webpage's frame document without navigating to this webpage

查看:103
本文介绍了无需导航到此网页即可访问网页的框架文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

标题可能会让人困惑,但这不仅仅是一种印象。
我对此有一个以前的帖子,但核心问题是实际上比这更深。



基本上,我有一个很大的代码,可以导航到我公司的Intranet网页(让我们称这个网页为开始网页)。

然后我提供用户名和密码信息,然后点击登录按钮。点击登录按钮将在新窗口中创建一个新的网页。我们会将这个新网页称为主页网页。



主网页包含我现在想要在开始网页时获得的所有重要信息对于我来说没用。



这里的第一个挑战是启动或选择主页面,因为开始页面仍处于激活状态。主网页显然有一个URL,但是不可能直接导航到这个网址,所以这就是为什么我要谈论网页的激活或选择。我设法通过 forum's post



如果您有任何疑问,请告诉我,但这不是本帖的问题。 / p>

因此,在激活主网页时,我想点击一个元素以显示更多信息。这个元素嵌入在 postfachcontent 框架中。
请参阅主页HTML文档概述放大部分点击



在我之前我试图做到这一点,但熟悉嵌入式的元素谁使任务更难。



在其他成员的帮助下,我想出了可以在那里有两种方法可以进入 postfachcontent 框架:




  1.   Set w = IEWindowFromLocation(path)

    Dim IEDoc As HTMLDocument
    Set IEDoc = w.document'w是我在代码

    中选择的所谓Main网页Dim SubFramesCollection作为HTMLWindow2
    Dim GoodFrame作为HTMLWindow2
    Dim Postfachcontent_Frame作为HTMLWindow2


    设置SubFramesCollection = IEDoc.frames'的长度这是3,因为它包含3个主框架
    Set GoodFrame = SubFramesCollection(1).frames'这包含了contentframe框架的2个框架,所以length = 2
    Set Postfachcontent_Frame = GoodFrame(1)

    Doc2 = Postfachcontent_Frame.document


但是,这里的问题是,一旦我访问了框架,我就困惑于如何真正选择表格的元素并点击它。


  1. 通过导航到新的网页,该网页将是主网页的简化版本,仅关注我感兴趣的框架,以便导航到主网页URL& contentframe .src(或 postfachcontent .src)。

在这里,正如我上面所说,是我无法直接导航到主网页,所以我想我可以尝试声明一个新的InternetExplorer窗口,并给它一个位置,而不实际导航到页面(但它不工作)。尝试如下:

  Set w = IEWindowFromLocation(path)

Dim IEDoc As HTMLDocument
Dim IEDok As HTMLDocument
Set IEDoc = w.document

$ b $ Dim ContentFramesCollection As IHTMLElementCollection
Dim ContentFrame As HTMLFrameElement
Dim PostFachContentFramesCollection As IHTMLElementCollection

设置ContentFramesCollection = IEDoc.getElementsByName(contentframe)'这个工作,并返回1个项目,这是称为contentframe的框架,所以它是包含1元素元素的集合
'MsgBox ContentFramesCollection.Length'返回1


如果不是ContentFramesCollection是Nothing然后

设置ContentFrame = ContentFramesCollection(0)'在这里,我们隔离MainFramesCollection中包含的唯一项并将其存储在一个单独的元素称为ContentFrame
MsgBox w.document.Location& ContentFrame.src
'On Error Resume Next
Set w2.document.Location = w.document.Location& ContentFrame.src
'MsgBox Err.Description'返回自动化错误未指定错误

设置IEDok = w2.document
设置PostFachContentFramesCollection = IEDok.getElementsByName(postfachcontent)
MsgBox PostFachContentFramesCollection.Length'返回0 ... oops

End If

感谢您接触到此行,并欢迎任何帮助!

解决方案

解决方案:对于那些可能感兴趣的人,的Dee和Tim Williams的这里一种方式点击包含onclick属性的多个可能元素之一。



对于贪婪的元素,这里是直接的一行解决方案

其中列出了可点击元素:

  IEDoc.frames(1).frames(1).document.getElementById(notPrintable  ).document.getElementsByName(result)(0).document.getElementsByTagName(td)

其中IEDoc是主网页的HTML文档。



备注:


  1. 为了得到这个最终的解决方案,我必须做一些测试,看看我处理的是哪种类型,以确保我知道我在做什么。您可以在下面的代码示例中看到这些测试步骤以及它们在MsgBox中的显示结果:

      MsgBox TypeName IEDoc.getElementsByName(contentframe)(0))'returns HTMLFrameElement 
    MsgBox IEDoc.getElementsByName(contentframe)(0).document.getElementsByName(postfachcontent)。Length'0
    MsgBox IEDoc .frames(1).frames.Length'returns 2
    MsgBox TypeName(IEDoc.frames(1).frames(1))'HTMLWindow2
    MsgBox TypeName(IEDoc.frames(1).frames(1 ).document.getElementById(notPrintable))'HTMLDivElement
    MsgBox TypeName(IEDoc.frames(1).frames(1).document.getElementById(notPrintable)。getElementsByName(result))'error
    MsgBox TypeName(IEDoc.frames(1).frames(1).document.getElementById(notPrintable)。document.getElementsByName(result))'DispElementcollection
    MsgBox TypeName(IEDoc.frames( 1).frames(1).document.getElementById

    (notPrintable)。document.getEleme ntsByName(result)(0))'HTMLFormElement
    MsgBox TypeName(IEDoc.frames(1).frames(1).document.getElementById(notPrintable)。document.getElementsByName(result)(0 ).getElementsByClassName(resultRow))'error
    MsgBox TypeName(IEDoc.frames(1).frames(1).document.getElementById(notPrintable)。document.getElementsByName(result)(0) .document.getElementsByClassName(resultRow))'error
    MsgBox TypeName(IEDoc.frames(1).frames(1).document.getElementById(notPrintable)。document.getElementsByName(result)(0 ).document)'HTMLDocument
    Result_Form_Doc = IEDoc.frames(1).frames(1).document.getElementById(notPrintable)。document.getElementsByName(result)(0).document'error
    IEDoc.frames(1).frames(1).document.getElementById(notPrintable)。document.getElementsByName(result)(0).Click'works ... but no click
    MsgBox TypeName (IEDoc.frames(1).frames(1).document.getElementById( notPrintable)。document.getElementsByName( 结果)(0).document.getElementsByTag Name(tr))'DispHTMLElementCollection
    MsgBox IEDoc.frames(1).frames(1).document.getElementById(notPrintable)。document.getElementsByName(result)(0).document.getElementsByTagName (1).frames(1).document.getElementById(notPrintable)。document.getElementsByName(result(tr))。长度'12 ...怪异的...我计数2
    IEDoc.frames(1) )(0).document.querySelector(td [onclick ='cView(62972172,'0',viewButton,'');'])。点击'error
    MsgBox TypeName(IEDoc.frames(1 ).frames(1).document.getElementById(notPrintable)。document.getElementsByName(result)(0).document.querySelector(td [onclick ='cView(62972172,'0',viewButton,'' );']))'error
    MsgBox IEDoc.frames(1).frames(1).document.getElementById(notPrintable)。document.getElementsByName(r​​esult)(0).document.getElementsByTagName (td)长度'42 ...计数34
    MsgBox IEDoc.frames(1).frames(1).document.getElementById(notPrintable)。document.getElementsByName(result)(0 ).document.getElementsByTag Name(table)。Length'4 ... count 1
    IEDoc.frames(1).frames(1).document.getElementById(notPrintable)。document.getElementsByName(result)(0 ).document.getElementsByTagName(td)(30).Click'WORKSSSSS
    MsgBox IEDoc.frames(1).frames(1).document.getElementById(notPrintable)。document.getElementsByName(result )(0).document.getElementsByTagName(td)。q​​uerySelectorAll(td [onclick ='cView(62972172,'0',viewButton,'');'])。Length'error
    MsgBox IEDoc .frames(1).frames(1).document.getElementById( notPrintable)。document.getElementsByName( 结果)(0).document.getElementsByTagName( TD)。querySelectorAll(TD [ROWSPAN ='1 '))。Length'error
    MsgBox TypeName(IEDoc.frames(1).frames(1).document.getElementById(notPrintable)。document.getElementsByName(r​​esult)(0).document。 getElementsByTagName(td))'DispHTMLElementCollection
    Dim Elem_td As IHTMLElement
    Dim el As Integer,ind As Integer
    Dim align_center_collection()
    el = 0
    ind =0
    对于每个Elem_td在IEDoc.frames(1).frames(1).document.getElementById(notPrintable)。document.getElementsByName(result)(0).document.getElementsByTagName(td)
    el = el + 1
    如果Elem_td.Align =centerThen
    ind = ind + 1

    End If

    下一个

    MsgBox el& 和& ind'如果onclick:42和0(预计34和16)
    '如果rowspan:42和42(预计34和34)
    '如果align = center:42和2(预计34和2) - >好
    '如果align = left:42和14(预计34和14) - >好的
    '如果rowSpan:42和42(预计34和34)




    1. 我注意到,在某些时候,VBA的Intellisense不会再帮助您了解可用的属性/方法,所以这就是为什么我必须经过几次测试

    2. 以某种方式完成所有这一切似乎是不可能的,因为即使typename返回一个有效类型,我也不能将它存储在同一类型的变量中。 我没有最终选择一个确切的元素,但是因为(就像你在图片上看到的那样)有几个可点击的元素,我没有做finition。



The title might be confusing, but it's not just an impression. I had a previous post about this but the core problem is actually deeper than this.

Basically I have a big code in which I navigate to an intranet webpage from my company ( let's call this webpage the Start webpage).

I then provide username and password information and click on the "login" button. Clicking on the login button will create a new webpage in a NEW window. We will call this new webpage the Main webpage.

The Main webpage contains all the important information I want to get while the Start webpage is now useless to me.

First challenge here was then to "activate" or "select" the Main webpage since the Start webpage was still activated. The Main webpage has obviously a URL but it's impossible to directly navigate to it, so that's why I talk about "activation" or "selection" of the webpage. I managed to do this with the help of that forum's post

If you have any questions about it let me know, but it's not the problem of this post.

So with the Main webpage activated, I want to click on an element to display more information. This element is embedded in the frame postfachcontent. See Main Page HTML document Overview and Zoom on part to click on.

In my previous post mentioned above, I tried to do this but got familiar with "embedded" elements who made the task harder.

With the help of other members, I figured out there could be 2 ways to get in the postfachcontent frame :

  1. By selecting child by child the frames :

       Set w = IEWindowFromLocation(path)
    
        Dim IEDoc As HTMLDocument
        Set IEDoc = w.document ' w is the so called Main webpage that I selected peviously in the code
    
      Dim SubFramesCollection As HTMLWindow2
      Dim GoodFrame As HTMLWindow2
      Dim Postfachcontent_Frame As HTMLWindow2
    
    
         Set SubFramesCollection = IEDoc.frames ' the length of this is 3 since it contains the 3 main frames
         Set GoodFrame = SubFramesCollection(1).frames ' this  contains the 2 frames of the "contentframe" frame so length = 2
         Set Postfachcontent_Frame = GoodFrame(1)
    
         Doc2 = Postfachcontent_Frame.document
    

But the problem here is that once I accessed to the frame I got confused on how to actually select the element of the table and click on it

  1. By "navigating" to a new webpage which would be a reduced version of the Main webpage only focusing on the frame I am interested in so navigating to Main Webpage URL & contentframe.src (or postfachcontent.src).

But the problem here, as I said above, is that I can't navigate to the Main webpage directly so I thought I could try to declare a New InternetExplorer window and give it a location without actually navigating to the page ( but it doesn't work unfortunately). Attempt below :

 Set w = IEWindowFromLocation(path)

    Dim IEDoc As HTMLDocument
    Dim IEDok As HTMLDocument
    Set IEDoc = w.document


 Dim ContentFramesCollection As IHTMLElementCollection
 Dim ContentFrame As HTMLFrameElement
 Dim PostFachContentFramesCollection As IHTMLElementCollection

  Set ContentFramesCollection = IEDoc.getElementsByName("contentframe") ' this works and returns 1 item which is the frame called contentframe, so it s a collection of element containing 1 element only
    ' MsgBox ContentFramesCollection.Length ' returns 1


  If Not ContentFramesCollection Is Nothing Then

       Set ContentFrame = ContentFramesCollection(0) ' Here we isolate the unique item contained in MainFramesCollection and store it in a single element called ContentFrame
       MsgBox w.document.Location & ContentFrame.src
      'On Error Resume Next
       Set w2.document.Location = w.document.Location & ContentFrame.src
      'MsgBox Err.Description ' returns automation error unspecified error

       Set IEDok = w2.document
       Set PostFachContentFramesCollection = IEDok.getElementsByName("postfachcontent")
       MsgBox PostFachContentFramesCollection.Length ' returns 0...oops

  End If

Thanks for reaching that line and any help is welcome !

解决方案

SOLUTION : For those it might interest, I found with the help of dee and the asnwer of Tim Williams on here a way to click on one of the multiple possible elements containing a "onclick" property.

For the greedy ones, here's the straight in-one-line solution which lists the clickable elements :

IEDoc.frames(1).frames(1).document.getElementById("notPrintable").document.getElementsByName("result")(0).document.getElementsByTagName("td")

where IEDoc is the HTML document of the Main webpage.

Remarks :

  1. In order to get this final solution, I had to do some tests to see which type I was dealing with to make sure I knew what I was doing. You can see these tests steps in the following code sample as well as their displayed result in the MsgBox in comment :

    MsgBox TypeName(IEDoc.getElementsByName("contentframe")(0)) ' returns HTMLFrameElement
        MsgBox IEDoc.getElementsByName("contentframe")(0).document.getElementsByName("postfachcontent").Length ' 0
        MsgBox IEDoc.frames(1).frames.Length ' returns 2
        MsgBox TypeName(IEDoc.frames(1).frames(1)) ' HTMLWindow2
        MsgBox TypeName(IEDoc.frames(1).frames(1).document.getElementById("notPrintable")) ' HTMLDivElement
        MsgBox TypeName(IEDoc.frames(1).frames(1).document.getElementById("notPrintable").getElementsByName("result")) ' error
        MsgBox TypeName(IEDoc.frames(1).frames(1).document.getElementById("notPrintable").document.getElementsByName("result")) 'DispElementcollection
         MsgBox TypeName(IEDoc.frames(1).frames(1).document.getElementById
    
    ("notPrintable").document.getElementsByName("result")(0)) ' HTMLFormElement
     MsgBox TypeName(IEDoc.frames(1).frames(1).document.getElementById("notPrintable").document.getElementsByName("result")(0).getElementsByClassName("resultRow")) ' error
      MsgBox TypeName(IEDoc.frames(1).frames(1).document.getElementById("notPrintable").document.getElementsByName("result")(0).document.getElementsByClassName("resultRow")) ' error
    MsgBox TypeName(IEDoc.frames(1).frames(1).document.getElementById("notPrintable").document.getElementsByName("result")(0).document) ' HTMLDocument
    Result_Form_Doc = IEDoc.frames(1).frames(1).document.getElementById("notPrintable").document.getElementsByName("result")(0).document ' error
    IEDoc.frames(1).frames(1).document.getElementById("notPrintable").document.getElementsByName("result")(0).Click ' works...but no click
    MsgBox TypeName(IEDoc.frames(1).frames(1).document.getElementById("notPrintable").document.getElementsByName("result")(0).document.getElementsByTagName("tr")) ' DispHTMLElementCollection
    MsgBox IEDoc.frames(1).frames(1).document.getElementById("notPrintable").document.getElementsByName("result")(0).document.getElementsByTagName("tr").Length ' 12...weird...I counted 2
    IEDoc.frames(1).frames(1).document.getElementById("notPrintable").document.getElementsByName("result")(0).document.querySelector("td[onclick='cView(62972172,'0', viewButton, '' );']").Click ' error
    MsgBox TypeName(IEDoc.frames(1).frames(1).document.getElementById("notPrintable").document.getElementsByName("result")(0).document.querySelector("td[onclick='cView(62972172,'0', viewButton, '' );']")) 'error
     MsgBox IEDoc.frames(1).frames(1).document.getElementById("notPrintable").document.getElementsByName("result")(0).document.getElementsByTagName("td").Length '42...counted 34
    MsgBox IEDoc.frames(1).frames(1).document.getElementById("notPrintable").document.getElementsByName("result")(0).document.getElementsByTagName("table").Length ' 4...counted 1
    IEDoc.frames(1).frames(1).document.getElementById("notPrintable").document.getElementsByName("result")(0).document.getElementsByTagName("td")(30).Click ' WORKSSSSS
    MsgBox IEDoc.frames(1).frames(1).document.getElementById("notPrintable").document.getElementsByName("result")(0).document.getElementsByTagName("td").querySelectorAll("td[onclick='cView(62972172,'0', viewButton, '' );']").Length ' error
    MsgBox IEDoc.frames(1).frames(1).document.getElementById("notPrintable").document.getElementsByName("result")(0).document.getElementsByTagName("td").querySelectorAll("td[rowSpan='1']").Length ' error
    MsgBox TypeName(IEDoc.frames(1).frames(1).document.getElementById("notPrintable").document.getElementsByName("result")(0).document.getElementsByTagName("td")) ' DispHTMLElementCollection
    Dim Elem_td As IHTMLElement
    Dim el As Integer, ind As Integer
    Dim align_center_collection()
    el = 0
    ind = 0
    For Each Elem_td In IEDoc.frames(1).frames(1).document.getElementById("notPrintable").document.getElementsByName("result")(0).document.getElementsByTagName("td")
    el = el + 1
        If Elem_td.Align = "center" Then
            ind = ind + 1
    
            End If
    
    Next
    
    MsgBox el & " and " & ind   ' if onclick : 42 and 0 (expected 34 and 16)
                                   ' if rowspan : 42 and 42(expected 34 and 34)
                                ' if align = center : 42 and 2 (expected 34 and 2) - > good
                                ' if align = left : 42 and 14 (expected 34 and 14) - > good
                                ' if rowSpan : 42 and 42 (expected 34 and 34)
    

    1. I noticed that at some point the Intellisense of VBA won't help you anymore to know the available properties/methods so that's why I had to go through a few tests
    2. It seems impossible somehow to do all this step by step because even though the typename returns a valid type, I can't store this in a variable of that same type.
    3. I didn't select one exact element finally, but since (as you can see on the picture) there are a few clickable, I didn't do the "finition".

这篇关于无需导航到此网页即可访问网页的框架文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆