【问题标题】:Cannot write in InputBox inside a frame of a website无法在网站框架内的 InputBox 中写入
【发布时间】:2014-12-16 14:17:32
【问题描述】:

这篇文章是Filling Internet Explorer inputbox的关注

我们试图找出问题所在,但我似乎无法(可能是由于页面中的 JavaScript?-请查看另一篇文章)正确处理我想要的特定 InputBox。我写的代码:

Sub AddInfoFromIntranet()

Dim Ie As SHDocVw.InternetExplorer
Dim Doc As MSHTML.HTMLDocument
Dim Elements As MSHTML.IHTMLElementCollection

    Set Ie = New SHDocVw.InternetExplorer
    With Ie
        .navigate "[HERE GOES MY WEBSITE]"
        .Visible = True

        Do Until Not .Busy And .readyState = 4
            DoEvents
        Loop

        Set Doc = .document
        Set Elements = Doc.getElementsByName("Nachnamevalue")

        .Quit
    End With

    Set Ie = Nothing

End Sub

“Nachnamevalue”是网页“top_window”框架中包含的元素的名称:

上图是我打开我想要的地址,按 F12 并单击所需输入框上的“选择项目”项。 一开始页面只是一堆javascript(打开页面-F12并复制DOM资源管理器的内容)

现在,如果我尝试查看我的“元素”变量是什么,我会得到一个基本上是空的对象。

如果我写:

Set Elements = Doc.getElementsByName("top_window")

有些东西要被发现了。 不幸的是,我正在处理一个内部网页面,所以我无法提供地址。

编辑:完整的javascript

<html><head><script language="JavaScript">

    function NewWindow(window_name){
        var detail; 
        detail = open(window_name,"detail","dependent,resizable,screenX=50,screenY=50,width=400,height=400");
        detail.focus();
    }

    function NewWindowScroll(window_name){
        var detail; 
        detail =    open(window_name,"detail","dependent,scrollbars,resizable,screenX=50,screenY=50,width=600,height=500");
        detail.focus();
    }


    function NewWindowSize(window_name,w,h){
        var detail, windata;
        detail = open(window_name,"detail","dependent,scrollbars,resizable,screenX=250,screenY=250,width=" + w + ",height=" + h);
        detail.focus();
    }

    function MiniWindow(window_name){
        var detail; 
        detail = open(window_name,"miniwindow","dependent=no,resizable=yes,screenX=0,screenY=0,width=800,height=20");
        detail.focus();
    }



    last_timer="new"    

    function DoSearch(){
        document.Suchform.submit() 
    }

    function wait_and_search(){ 
        clearTimeout(last_timer)
        last_timer = window.setTimeout("DoSearch()", 400);
    }


    function check_kpeq(this_handle){
        equ_string = this_handle.value;
        text = equ_string.replace(/[^0-9A-Za-z\-]/g,"");    
        zahlen = text.split(/[\-\/]/);              
        if (zahlen.length != 4) {falsches_equ_format(this_handle);  return false;}

        fill="000";

        Zahl_1   = fill + zahlen[0];    
        Zahl_1 = Zahl_1.slice(Zahl_1.length-3)

        Zahl_2   = fill + zahlen[1];    
        Zahl_2 = Zahl_2.slice(Zahl_2.length-1)

        Zahl_3   = fill + zahlen[2];    
        Zahl_3 = Zahl_3.slice(Zahl_3.length-2)

        Zahl_4 = zahlen[3];

        this_handle.value = (Zahl_1 + "-" + Zahl_2 + "-" + Zahl_3 + "-" + Zahl_4);

        return true;      
    }


    function check_anab(this_handle){
        anab_string = this_handle.value;
        if (anab_string == "") {return true;} 
        text = anab_string.replace(/[^0-9\-]/g,""); 
        zahlen = text.split(/[\-\/]/);              
        if (zahlen.length != 3) {falsches_anab_format(this_handle);  return false;}

        fill="000";

        Zahl_1   = fill + zahlen[0];
        Zahl_1 = Zahl_1.slice(Zahl_1.length-2)

        Zahl_2   = fill + zahlen[1];    
        Zahl_2 = Zahl_2.slice(Zahl_2.length-2)

        Zahl_3   = fill + zahlen[2];    
        Zahl_3 = Zahl_3.slice(Zahl_3.length-3)


        this_handle.value = (Zahl_1 + "-" + Zahl_2 + "-" + Zahl_3 );

        return true;      
    }


    function falsches_anab_format(this_handle) {
        t1 = "Ung?ltiges Format f?r HVT AN/AB:  >>  " + this_handle.value + "  <<\n\n";
        t2 = "Nur 3 Zahlen mit '-' als Trennzeichen erlaubt\n\n";
        t3 = "Z.B.: '01-08-001'"
        alert(t1 + t2 + t3);
        this_handle.focus();
    }


    function falsches_equ_format(this_handle) {
        t1 = "Ung?ltiges Format f?r EQU:  >>  " + this_handle.value + "  <<\n\n";
        t2 = "Nur 4 Zahlen mit '-' als Trennzeichen erlaubt\n\n";
        t3 = "Z.B.: '003-2-60-7'"
        alert(t1 + t2 + t3);
        this_handle.focus();
    }


    function check_form(){

        if (check_kpeq(document.editanlagedata.KPEQ)        == false){return false};        
        if (check_anab(document.editanlagedata.HVAN)        == false){return false};        
        if (check_anab(document.editanlagedata.HVAB)        == false){return false};        
        return true;
    }

</script>


    <title>Telefon-Suche</title>    
    <link title="Telefonsuche - Abteilung" href="[XML FILE REGARDING WEBPAGE].xml" rel="search" type="application/opensearchdescription+xml">
    <link title="Telefonsuche - Name" href="[XML FILE REGARDING WEBPAGE].xml" rel="search" type="application/opensearchdescription+xml">
</head>
<!-- frames -->
<frameset rows="90,*" bordercolor="#ffffcc" frameborder="0">
    <frame name="top_window" src="index.cfm?fuseaction=StdSearchForm" scrolling="No">
    <frame name="bottom_window" src="index.cfm?fuseaction=ShowStartpage" scrolling="Auto">
</frameset><noframes></noframes>

【问题讨论】:

  • 请添加javascript代码
  • @Noldor130884 请将观察者的图片也添加到对象中,正如您在聊天中向我展示的那样,以向其他用户显示内容。
  • 在使用 SHDocVw 和 MSHTML 对象时我应该参考哪个库?
  • “Microsoft Internet 控件”和“HTML 对象库”

标签: javascript vba internet-explorer iframe web-scraping


【解决方案1】:

您遇到的问题是,当您的目标输入框“Nachnamevalue”位于IFrame 内时,IE 必须先导航到目标页面,然后再导航到 IFrame

如果没有导航到IFrame,则DOM 不包含IFrame 内部的元素,这就是它不起作用的原因。高温

(示例代码使用位于我的 PC 上的文件,因此将 url 替换为您来自 Intranet 的实际 url,并将 'main.html' 的名称替换为实际页面的名称。)

Option Explicit

' Add reference to Microsoft Internet Controls (SHDocVw)
' Add reference to Microsoft HTML Object Library

Sub AddInfoFromIntranet()

    Dim ie As SHDocVw.InternetExplorer
    Dim doc As MSHTML.HTMLDocument
    Dim url As String

    ' Navigate to main page first
    url = "file:///c:/temp/Noldor/"
    Set ie = New SHDocVw.InternetExplorer
    ie.Visible = True
    ie.navigate url & "main.html"

    While ie.Busy Or ie.readyState <> READYSTATE_COMPLETE: DoEvents: Wend

    Dim inputs As MSHTML.IHTMLElementCollection
    Dim iFrames As MSHTML.IHTMLElementCollection
    Dim iFrame As MSHTML.HTMLFrameElement

    ' Get top_window frame and navigate to it then
    Set doc = ie.document
    Set iFrames = doc.getElementsByName("top_window")

    If Not iFrames Is Nothing Then
        Set iFrame = iFrames(0)
        ie.navigate url & iFrame.src

        While ie.Busy Or ie.readyState <> READYSTATE_COMPLETE: DoEvents: Wend

        Set inputs = doc.getElementsByName("Nachnamevalue")
        If Not inputs Is Nothing Then
            inputs(0).Value = "Test123"
        End If
    End If

    ie.Quit
    Set ie = Nothing
End Sub

main.html 的 HTML 示例:

<html>
<head>
<title></title>
</head>
<frameset cols="" rows="">
  <frame name="top_window" src="top_window.html" />
</frameset>
<noframes>
<!-- no frames content here -->
</noframes>
</html>

top_window.html 的 HTML 示例:

<html>
<head>
<title></title>
</head>
<body bgcolor="#ffffcc">
  <form name="Suchform" action="index.cfm">
    Nachname:
    <input name="Nachnamevalue" type="text" size="8">
  </form>
</body>
</html>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-02-06
    • 1970-01-01
    • 2011-07-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-09
    相关资源
    最近更新 更多