本文介绍了自动化Internet Explorer:输入一个没有“ID”的值。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用excel VBA来自动化输入到我公司的Intranet网页上的数据。

 < input我知道如何与网页的值进行交互,字段具有id name =txtUserNametabindex =1id =txtUserNametype =text> 

有了这样的html代码,我会使用类似于

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

我遇到的问题是与我试图交互的字段相关的代码是

 < HTML>< HEAD>< META content =IE = 5.0000http-equiv =X-UA-Compatible> 
< SCRIPT type = text / javascript>
函数ajusterFrames(){
var iLargeurGauche = 217;
var iLargeurDroite = 850;
var iLargeurFenetre = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;
var iMarge = 0;
var sCols =; (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:600pxname =recherche>
< INPUT onclick = afficherRecherche(this.value); type = radio value =简单名称= TypeRecherche>
< / DIV><!------ RECHERCHEAVANCÉ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这是代码我可以想出来,我尝试了很多版本,但仍然无法正确使用。

包含解决方案的更新代码

  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)'这是我需要的值输入

'创建InternetExplorer对象
设置IE = CreateObject(InternetExplorer.Application)

'您可以取消下一行查看表单结果
IE.Visible = True

'发送表单数据到URL作为POST二进制请求
IE.Navigatehttp://intranet.cima.ca/fr/application/paq/projets /index.asp?v1_lang=1

等待IE浏览器加载...
Do while IE.Busy
Application.Wait DateAdd(s,1,Now)
Loop

'更新后的代码

  Set links = IE.Document.frames(2).Document.getElementsByTagName( 输入)
n = links.Length
虽然i< n
如果链接(i).Name =txtMotCle则
链接(i).Value = num_proj
结束如果
i = i + 1
Wend

End Sub

任何人都可以帮助我吗?

另外,如果我可能需要单击表单底部的搜索按钮,但我无法识别它的语法,因此我不知道如何与它进行交互。有人能告诉我如何点击具有此html代码的按钮吗?


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

这是更新的代码解决方案:

<$设置链接= IE.Document.frames(2).Document.getElementsByTagName(a)
n = links.LengtH
i = 0
虽然我< n
如果链接(i).href =javascript:submitForm('avancee');然后
Set objElement = links(i)
objElement.ClicK
End If
i = i + 1
Wend

非常感谢您花费时间回答我的问题。

解决方案

编辑:输入元素包含在一个框架中,这就是为什么@ jeeped的答案不起作用。 getElementsByTagName(input)应该返回它们。希望这个编辑能够让你不必阅读评论就可以找到答案。要返回帧中包含的输入,请使用

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

其中x是框架的索引。



原始回复如下。



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



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

我个人可能会在项目的引用中包含Microsoft Internet Controls和Microsoft HTML Object Library,以便我可以使用早期绑定并声明变量,然后使用对于每个迭代:

  Dim inputs As MSHTML.IHTMLElementCollection 
Dim inpt As MSHTML.IHTMLInputElement
Dim IE As New SHDocVw.InternetExplorer

IE.Visible = True
IE.navigatehttp://intranet.cima.ca/fr/application/paq/projets/index.asp? v1_lang = 1
虽然IE.readyState<> READYSTATE_COMPLETE:DoEvents:Wend

设置输入= IE.document.getElementsByTagName(input)
For Each inpt输入
如果inpt.Name =txtNoProjet则输入。 Value = num_proj
Next

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

 如果inpt.getAttribute(name)=txtNoProjet,那么inpt.Value = num_proj 


I'm trying to use excel VBA to automate the data entering on a intranet Webpage of my company. I know how to interact with values of a web page the fields have "id" like in the html code below

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

With that kind of html code I would use something like

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

The problem I'm facing is that the code associated with the field I'm trying to interact is

        <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>

So I'm trying to modify the value of the field identify by: "txtNoProjet" Here's the code I could come up with, I tried many versions around this and still can't get it right.
UPDATED CODE TO INCLUDE SOLUTION

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

'UPDATED CODE BELOW

    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 

Can anyone help me with this?
And also if I may I will the need to click the button "search" on the bottom of the form , but I can't recognize it's syntax so I don't know how to interact with it. Could someone tell me how to click the button that has this html code?

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

UPDATED CODE SOLUTION FOR THIS IS:

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

Thank you so much in advance for the time you'll spend answering my questions.

解决方案

Edited: the input elements were contained in a frame, which is why @jeeped's answer wasn't working. getElementsByTagName("input") should have returned them. Hopefully this edit will save you from having to read through the comments to find that out. To return inputs contained in a frame, use

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

where x is the index of the frame.

Original response below.

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

If it's zero-based, shouldn't you remove the -1 that's preventing the loop from running? That is, if there is one element, then the for loop would be iNPT = 0 to 0 and execute once. If there were two, then the .length would return 1 and =0 to 1 would execute twice, as expected.

I personally would probably include both Microsoft Internet Controls and Microsoft HTML Object Library in the references for the project so that I could use early binding and declare the variables, and then use a for each to iterate:

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

Be aware that the HTML Object Library includes several different IHTMLElementCollections, with different numbers. In some situations you might need to use one of those instead. Also, you may need to use getAttribute to access the attribute:

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

这篇关于自动化Internet Explorer:输入一个没有“ID”的值。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-26 08:06