自动化Internet Explorer:input一个没有“ID”的值

我正在尝试使用excel VBA来自动化在我公司的Intranet网页上input的数据。 我知道如何与网页的值进行交互的字段有像下面的HTML代码中的“ID”

<input name="txtUserName" tabindex="1" id="txtUserName" type="text"> 

有了这样的HTML代码,我会使用类似的东西

 IE.Document.getElementbyId("txtUserName").Value = "UserName" 

我面临的问题是与我试图互动的领域相关的代码是

  <HTML><HEAD><META content="IE=5.0000" http-equiv="X-UA-Compatible"> <SCRIPT type=text/javascript> function ajusterFrames() { var iLargeurGauche = 217; var iLargeurDroite = 850; var iLargeurFenetre = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth; var iMarge = 0; var sCols = ""; if (iLargeurFenetre > (iLargeurGauche + iLargeurDroite)) { iMarge = (iLargeurFenetre - (iLargeurGauche + iLargeurDroite)) / 2;} sCols = (iLargeurGauche + iMarge) + ",*"; document.getElementById("framesetbas").cols = sCols; document.getElementById("cadres").style.display = "block";} window.onload = ajusterFrames; window.onresize = ajusterFrames; </SCRIPT> 

  <TBODY> <TR> </TR> <TR> <TD> <!--RECHERCHE--> DIV id=Projet> <!--RECHERCHE--> <CENTER> <FIELDSET style="WIDTH: 600px" name="recherche"> <LEGEND style="FONT-SIZE: 10px; FONT-FAMILY: verdana; COLOR: #767676" name="legende_recherche"> <INPUT onclick=afficherRecherche(this.value); type=radio value=simple name=TypeRecherche> </DIV><!------RECHERCHE AVANCÉE-------------------------------------> <DIV id=divAvancee> <CENTER> <TABLE cellSpacing=0 cellPadding=0 border=0> <TBODY> <TR> <TR> <TR> <TR> <TR> <TR> <TD align=right> <FONT color=#003366 size=1 face=verdana>No Projet :</FONT></TD> 

所以我试图修改字段标识的值:“txtNoProjet”这里是我可以提出的代码,我尝试了很多版本,仍然不能正确的。
更新代码,包括解决scheme

 Private Sub entree_budget() Dim i As Long Dim IE As Object Dim Doc As Object Dim objElement As Object Dim objCollection As Object Dim buttonCollection As Object Dim valeur_heure As Object num_proj = Cells(1, 3) 'this is the value that I need to input ' Create InternetExplorer Object Set IE = CreateObject("InternetExplorer.Application") ' You can uncoment Next line To see form results IE.Visible = True ' Send the form data To URL As POST binary request IE.Navigate "http://intranet.cima.ca/fr/application/paq/projets/index.asp?v1_lang=1" ' Wait while IE loading... Do While IE.Busy Application.Wait DateAdd("s", 1, Now) Loop 

'更新后的代码

  Set links = IE.Document.frames(2).Document.getElementsByTagName("input") n = links.Length While i < n If links(i).Name = "txtMotCle" Then links(i).Value = num_proj End If i = i+ 1 Wend End Sub 

谁能帮我这个?
另外,如果我可能需要单击表单底部的“search”button,但我无法识别它的语法,所以我不知道如何与之交互。 有人可以告诉我如何点击这个HTML代码的button?

 <A href="javascript:submitForm('avancee');"> <IMG border=0 alt="Rechercher un projet" src="../../../images/fr/rechercher.gif"></A> 

更新的代码解决scheme是:

 Set links = IE.Document.frames(2).Document.getElementsByTagName("a") n = links.LengtH i = 0 While i < n If links(i).href = "javascript:submitForm('avancee');" Then Set objElement = links(i) objElement.ClicK End If i = i + 1 Wend 

提前感谢您花费时间回答我的问题。

编辑:input元素被包含在一个框架,这就是为什么@ jeeped的答案不工作。 getElementsByTagName(“input”)应该返回它们。 希望这个编辑将使您不必阅读通过评论来发现。 要返回帧中包含的input,请使用

 IE.document.frames(x).document.getElementsByTagName("input") 

其中x是帧的索引。

原始回应如下。

================================================== =====

如果它是基于零的,不应该删除阻止循环运行的-1吗? 也就是说,如果有一个元素,则for循环将是iNPT = 0 to 0并执行一次。 如果有两个,那么.length将返回1,并且=0 to 1将按预期执行两次。

我个人可能会在项目的引用中包含Microsoft Internet控件和Microsoft HTML对象库,以便我可以使用早期绑定并声明variables,然后使用for each迭代:

 Dim inputs As MSHTML.IHTMLElementCollection Dim inpt As MSHTML.IHTMLInputElement Dim IE As New SHDocVw.InternetExplorer IE.Visible = True IE.navigate "http://intranet.cima.ca/fr/application/paq/projets/index.asp?v1_lang=1" While IE.readyState <> READYSTATE_COMPLETE: DoEvents: Wend Set inputs = IE.document.getElementsByTagName("input") For Each inpt In inputs If inpt.Name = "txtNoProjet" Then inpt.Value = num_proj Next 

请注意,HTML对象库包含几个不同的IHTMLElementCollections,其编号不同。 在某些情况下,您可能需要使用其中一种。 另外,您可能需要使用getAttribute来访问属性:

 If inpt.getAttribute("name") = "txtNoProjet" then inpt.Value = num_proj 

元素集合的索引是从零开始的 。 假设您只使用一个txtNoProjet

 IE.Document.getElementsbyName("txtNoProjet")(0).Value = num_proj 

我以前遇到过.getElementsbyName问题。 如果上述不适用于您,请收集所有<input>元素并遍历它们直到find所需的元素。

 dim iNPT as long for iNPT=0 to (IE.Document.getElementsbyTagName("input").length - 1) if IE.Document.getElementsbyTagName("input")(iNPT).name = "txtNoProjet" then IE.Document.getElementsbyTagName("input")(iNPT).value = num_proj exit for end if next iNPT 

其中一个应该让你去。 你遇到的最大的问题将是重复的名字。

编辑:单击图像锚的附录

 dim iIMG as long for iIMG=0 to (IE.Document.getElementsbyTagName("img").length - 1) if CBool(InStr(1, IE.Document.getElementsbyTagName("img")(iIMG).src, "rechercher.gif", vbTextCompare)) then IE.Document.getElementsbyTagName("img")(iIMG).click do while IE.Busy Or IE.ReadyState <> READYSTATE_COMPLETE: Do Events: Loop exit for end if next iIMG 

我通常更喜欢更接近于IE.Document.getElementsbyTagName("form")(0).submit但是您没有提供足够的HTML源代码来正确编写。 您不得不调整该代码,以查看您的.Frames元素。