获取ID /名称以外的属性

时间:2014-05-21 18:06:46

标签: vba internet-explorer excel-vba excel

我正在尝试编写一些VBA来将单元格值输入网站。

HTML的一个例子是:

<input name="ctl00$CPH$c_7f0de753x200dx402bxb63bxd08cc4f3f1e0"
   type="text" 
   id="c_7f0de753x200dx402bxb63bxd08cc4f3f1e0" 
   field-name="Forename" 
   map="contact_Forename" validate="required: true"> 

我已成功使用下面显示的ID和名称,但这些可能会发生变化,因此我需要在上面的HTML中引用map或field-name属性。

Sub Registration()
the_start:
On Error GoTo The_End
Set objie = CreateObject("internetexplorer.application")
objie.Top = 0
objie.Left = 0
objie.Width = 800
objie.Height = 600
objie.Visible = True

objie.navigate ("http://closerstill.circdata-solutions.co.uk/microsites/rfg/publish/PF2014/?source=websitetabpb")

Do

DoEvents

    If Err.Number <> 0 Then
        objie.Quit
        Set objie = Nothing
        GoTo the_start:

    End If

Loop Until objie.ReadyState = 4

objie.Document.getelementbyID("c_a5f795c9x0984x4bffxab24xae4ed7ccc093").Value = "Test"

The_End:
End Sub

1 个答案:

答案 0 :(得分:0)

Sub Registration1()
the_start:
On Error GoTo The_End
Set objie = CreateObject("internetexplorer.application")
objie.Top = 0
objie.Left = 0
objie.Width = 800
objie.Height = 600
objie.Visible = True

objie.navigate ("http://closerstill.circdata-solutions.co.uk/microsites/rfg/publish/PF2014/?source=websitetabpb")

Do

DoEvents

    If Err.Number <> 0 Then
        objie.Quit
        Set objie = Nothing
        GoTo the_start:

    End If

Loop Until objie.ReadyState = 4

Dim element As Object
Set Inputs = objie.document.getelementsbytagname("input")

For Each element In Inputs
If element.getattribute("field-name") = "Forename" Then
element.Value = "test"
End If

Next

The_End:
End Sub