2015-05-13 75 views
0

首先,代码如下:(该代码与vba-open-website的标准代码没有区别,如果这样可以节省您的时间阅读时间,我会放置一个< - --------这里来指示我把我的密码)VBA document.all.item仅适用于用户名,但不适用于密码

Sub Login_2Wiki() 

Dim ieApp As InternetExplorer 
Dim ieDoc As Object 
Dim ieTable As Object 
Dim clip As DataObject 
Dim iusr As Object 
Dim ipwd As Object 
Dim iform As Object 

'create a new instance of ie 
Set ieApp = New InternetExplorer 

'you don’t need this, but it’s good for debugging 
ieApp.Visible = True 

'assume we’re not logged in and just go directly to the login page 
ieApp.Navigate "https://xxx.xxxxx.com/login.action?" 
Do While ieApp.Busy: DoEvents: Loop 
Do Until ieApp.ReadyState = READYSTATE_COMPLETE: DoEvents: Loop 

Set ieDoc = ieApp 

'fill in the login form – View Source from your browser to get the control names 
With ieDoc 
Set iform = .Document.getElementById("login-container") 
Set iusr = iform.Document.getElementById("os_username") 
iusr.Value = "guest" 
Set ipwd = iform.Document.getElementById("os_password") 
ipwd.Value = "abc" 

.Document.getelementsbyname("loginButton")(0).Click 
End With 
Do While ieApp.Busy: DoEvents: Loop 
Do Until ieApp.ReadyState = READYSTATE_COMPLETE: DoEvents: Loop 


'close 'er up 
ieApp.Quit 
Set ieApp = Nothing 

End Sub 

和这里的网页上的源代码的一部分(这是一个Atlassian的合流页面,如果该事项):

<div class="field-group 
" 
    > 
<label id="os_username-label" for="os_username"> 
Username 
    </label> 

      <input type="text" name="os_username" id="os_username" class="text " placeholder="Username" data-focus="0"  /> 
        </div>       

<div class="field-group"> 
<label id="os_password-label" for="os_password"> 
Password 
    </label> 
<input type="password" name="os_password" id="os_password" class="password " placeholder="Password" /> 
       </div>       

问题:代码只填写了用户名,而没有在网页上填写密码。它没有提供任何错误。另外我注意到,在网页的源视图中有一个data-focus =“0”,并想知道是否是导致问题的原因。

编辑:我更换.Document.all.Item.Document.getElementById方法,althought代码可以找到元素os_password(没有错误),也无法分配值到它。 os_username与以前一样工作。

回答

1

尝试:

With .Document.getElementById("os_password") 
    .Focus() 
    .Value = "abc" 
End With 
+0

@TimVilliams嘿添,该.Focus()给出语法错误,这只是针对VB.net可能? – Alex

+0

@TimVilliams我用.Focus()= True编辑过,它可以消除语法错误,但密码字段仍然是空的 – Alex

+0

我不能在没有看到完整页面源的情况下提出更多建议。 –

1

我不认为重点有什么与你的问题。这就像说你不能Range("B1") = "abc"如果A1是ActiveCell。只要您直接将字符串传送到.Value属性,重点就不应该成为问题。如果你正在发送按键,那么或许焦点变得重要,但我们不打算这样做。

我遇到了一个类似的情况,它是一个多用途的登录页面。同一个“登录页面”为买卖双方提供了单独的登录表单。我怀疑仔细检查该页面后面的HTML会显示实际上有两个具有相同名称/ ID的元素(例如os_password)。除此之外,.Document.all.Item("something")方法可用于名称或ID;不是分配值的最权威的方法。看来你正在设置一些标识为os_password;只是不正​​确os_password

如果出现这种情况,您将不得不将输入的密码作为您想要处理的表单的子项输入,而不是整个文档主体。如果您有形式Second_Form的ID,那么你就可以访问输入字段这样的:

dim eFRM as MSHTML.IHTMLElement 
    set eFRM = ieApp.document.getElementById("Second_Form") 
    eFRM.getElementById("os_username").Value = "guest" 
    eFRM.getElementById("os_password").Value = "abc" 
    'don't use names to locate elements - horribly unreliable 
    eFRM.submit 
+0

嗨Jeeped谢谢你的回答。我在整个源页面上进行了搜索,并且这个问题上显示的4个'os_password'是唯一的匹配项。另外,两个os_username/os_pasword都采用“loginform”相同的形式。我用你的代码,它给了我错误438 - Obj不支持.Value行上的这个属性或方法 – Alex

+0

哦,那是我的2¢。在极少数情况下,即使是一个盲人也可以击中靶心,但这不是其中之一。 – Jeeped

+0

没有你的方法是绝对正确的 - 在分配前定位正确的组。 – Alex

相关问题