为什么我不能用VBA访问这个popup框的内容?
这里是popup框的html:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <!-- BEGIN /layouts/new_empty_layout.jsp --> <html> <head> <title>SERFF - System for Electronic Rate and Form Filing</title> <!-- Copyright 2016 National Association of Insurance Commissioners --> <link rel="stylesheet" type="text/css" href="/styles/serff-core.css"/> <link rel="stylesheet" type="text/css" href="/styles/core-header.css"/> <link rel="stylesheet" type="text/css" href="/styles/core-content.css"/> <link rel="stylesheet" type="text/css" href="/scripts/jquery-ui-css/custom-theme/jquery-ui-1.10.4.custom.css"/> <script type="text/javascript"> var staticUrlPath = '/'; var appUrlPath = '/serff/'; </script> <script type="text/javascript" src="/scripts/jquery-1.12.1.min.js"></script> <script type="text/javascript" src="/scripts/jquery.textarea.pack.js"></script> <script type="text/javascript" src="/scripts/jquery-ui-1.10.0.min.js"></script> <script type="text/javascript" src="/scripts/datepicker.js"></script> <script type="text/javascript" src="/scripts/default_layout.js"></script> <script> // custom SERFF dimensions: // dimension1 = SERFF User Type custom GA dimension var analyticsEnabled = true; // insert the analytics script and initialize the Google Analytics "ga" object if(analyticsEnabled){ var currentScript = document.currentScript || (function() { var scripts = document.getElementsByTagName('script'); return scripts[scripts.length - 1]; })(); var gaScript = document.createElement('script'); gaScript.async = true; gaScript.src = '/public/lib/ga/analytics.js'; document.getElementsByTagName("head")[0].insertBefore(gaScript, currentScript); var ga = window.ga = window.ga || function(){(window.ga.q = window.ga.q || []).push(arguments)}; window.ga.l = +new Date(); window.GoogleAnalyticsObject = 'ga'; ga('create', 'UA-5133885-7', 'auto'); } function analyticsPageView() { doSendAnalytics(function() { ga('send', 'pageview'); }); } function analyticsPageViewWithUserType() { doSendAnalytics(function() { ga('send', 'pageview', { 'dimension1' : getUserType() }); }); } function analyticsEvent(eventCategory, eventAction, callback){ if(callback){ doSendAnalytics(function() { ga('send', 'event', eventCategory, eventAction, {'hitCallback': callback}); }); } else { doSendAnalytics(function() { ga('send', 'event', eventCategory, eventAction); }); } } function analyticsEventWithUserType(eventCategory, eventAction, callback){ if(callback){ doSendAnalytics(function() { ga('send', 'event', eventCategory, eventAction, {'hitCallback': callback, 'dimension1' : getUserType()}); }); } else { doSendAnalytics(function() { ga('send', 'event', eventCategory, eventAction, {'dimension1' : getUserType()}); }); } } function getUserType(){ var userType = 'I'; if(userType == 'S'){ userType = 'State User'; } else if(userType == 'I') { userType = 'Industry User'; } else if (userType == 'N') { userType = 'NAIC Admin User'; } return userType; } // only send a message to GA if enabled in the INI file function doSendAnalytics(method){ if(analyticsEnabled){ method(); } } </script> <link rel="stylesheet" type="text/css" href="/styles/forms2.css"/> <!-- BEGIN /layouts/iefixes.jsp --> <!-- END /layouts/iefixes.jsp --> </head> <body> <div id="content"> <div id="main" style="background: none;"> <!-- Copyright 2014 National Association of Insurance Commissioners --> <!-- BEGIN /filing/ --> <html> <head> <title>SERFF - System for Electronic Rate and Form Filing</title> <link rel="stylesheet" type="text/css" href="/styles/serff-core.css" /> <link rel="stylesheet" type="text/css" href="/styles/core-header.css" /> <link rel="stylesheet" type="text/css" href="/styles/core-content.css" /> <link rel="stylesheet" type="text/css" href="/styles/forms2.css" /> <!-- BEGIN /layouts/iefixes.jsp --> <!-- END /layouts/iefixes.jsp --> <script language="javascript"> var uploadSubmitted = false; $( document ).ready(function() { $( 'form' ).submit(function( event ) { if (uploadSubmitted) { alert( "You have already performed this action. Please wait for the page to refresh." ); event.preventDefault(); } else { uploadSubmitted = true; } }); }); var skipcycle = false; function updateAttachments() { var groupId = window.opener.selectedAttachmentGroup; if (uploadForm.groupId.value == null || uploadForm.groupId.value ==''){ uploadForm.groupId.value = groupId; } var index = window.opener.selectedIndex; if (uploadForm.index.value == null || uploadForm.index.value ==''){ uploadForm.index.value = index; } } function focusOnMe(){ if (!skipcycle){ window.focus(); } mytimer = setTimeout('focusOnMe()', 500); } </script> </head> <body onload = "mytimer = setTimeout('focusOnMe()', 500);updateAttachments();"> <h2>SERFF File Attachment Upload</h2> <div class="documentArea"> <form name="uploadForm" method="POST" action="/serff/uploadFileAttachment.do" class="styled" enctype="multipart/form-data" id="uploadForm"> <input type="hidden" name="groupId" /> <input type="hidden" name="index" /> <input type="hidden" name="attachmentType" value="0"> <div class="uploadWarning" style="width:325px"> Attachments larger than 3MB or Microsoft Word(tm) documents cannot be uploaded to SERFF. </div> <div class="fieldgroup">File 1: <input type="file" name="contentIndexed[0]" value="" onfocus="skipcycle=true" class="inputFile"></div> <div class="fieldgroup">File 2: <input type="file" name="contentIndexed[1]" value="" onfocus="skipcycle=true" class="inputFile"><br /> </div> <div class="fieldgroup">File 3: <input type="file" name="contentIndexed[2]" value="" onfocus="skipcycle=true" class="inputFile"><br /> </div> <div class="fieldgroup">File 4: <input type="file" name="contentIndexed[3]" value="" onfocus="skipcycle=true" class="inputFile"><br /> </div> <div class="fieldgroup">File 5: <input type="file" name="contentIndexed[4]" value="" onfocus="skipcycle=true" class="inputFile"><br /> </div> <br /> <div class="actionButtons"> <input type="submit" value="Upload" class="inputSubmit"> <input type="button" class="inputButton" onclick="window.close();" value="Cancel" /> </div> </form> </div> </body> </html> <!-- END /filing/fileUpload.jsp --> </div> <!-- end main div --> </div> <!-- end content div --> </body> </html> <!-- END /layouts/new_empty_layout.jsp -->
我试图让上传文件对话框出现,通过使用vba来“点击”其中一个浏览button。 问题是vba不会让我在uploadForm中findinput标签。
我自己的代码:
Set uploadForm = ieUpload.document.getElementById("uploadForm") For Each inputTag In uploadForm.getElementsByTagName("input") If inputTag.Name = "contentIndexed[0]" Then inputTag.Click Next inputTag
VBA在for循环中引发“对象variables或块variables未设置”错误。
在聊天讨论后编辑
原来这个文件还没有完成加载。 检查ieUpload.Busy
伎俩。 例如,看这个问题 ,它在DoEvents
循环中检查Busy
。
原始答案
首先:
- 确保你的源文件的顶部有
Option Explicit
。 - 确保你已经添加了对象引用到你正在使用的HTML库。 除非没有别的select,否则不要使用
CreateObject
! - 确保你已经使用
Dim
来给所有的variables赋予一个特定的types – 不是Variant
或Object
。
现在,假设你正在使用MSHTML
,没有函数document.get
Elements
ById
。 你想要什么是Element
ById
,没有“元素”上的“s”。 通过代码单步骤 – 我怀疑uploadForm
Nothing
时,你点击For循环,所以uploadForm.getElementsByTagName
是引发错误。
编辑为什么我提到添加上面的参考? 因为如果你已经添加了引用,并且你已经说Dim uploadForm as MSHTML.IHTMLElement2
,那么VBA编辑器会自动将案例更改为uploadForm.getElementsByTagName
。 既然它没有,我想你没有Dim
的地方,或者它不是一个具体的Dim
。
(当然,除非你创build了一个名为getelementsbytagname
的variables,你不应该这么做:))
这是一个MSHTML的例子,从这个答案链接。