使用Excel vba登录javascript网页
我已经浏览了几个论坛,但不幸的是我的html技能不是他们应该具备的。有人能帮我找到使用Excel VBA登录www.emo.no的正确命令吗?请参阅底部的代码 我已成功地在谷歌搜索栏中填入以下代码:使用Excel vba登录javascript网页,javascript,excel,vba,Javascript,Excel,Vba,我已经浏览了几个论坛,但不幸的是我的html技能不是他们应该具备的。有人能帮我找到使用Excel VBA登录www.emo.no的正确命令吗?请参阅底部的代码 我已成功地在谷歌搜索栏中填入以下代码: Sub Google_Test() Set ie = CreateObject("InternetExplorer.application") Bnavn = "UserNa" With ie .Visible = True .navigate "www.google.com"
Sub Google_Test()
Set ie = CreateObject("InternetExplorer.application")
Bnavn = "UserNa"
With ie
.Visible = True
.navigate "www.google.com"
Do While .Busy Or _
.readyState <> 4
DoEvents
Loop
ie.document.all("q").Value = Bnavn
Do While .Busy Or _
.readyState <> 4
DoEvents
Loop
End With
Set ie = Nothing
End Sub
我正在尝试将Excel VBA登录到网站www.emo.no,但找不到要填写的正确字段
我试图让Excel VBA登录到的网页上的代码是:
<div id="PcLogin">
<script language="Javascript">
var locked = false;
function doLogin() {
document.login.uid.value = document.login.uid.value.toLowerCase();
document.login.pwd.value = document.login.pwd.value.toLowerCase();
return true;
}
if(!document.all){
document.captureEvents(Event.CLICK | Event.FOCUS);
}
/* Code for putting the visible word "Username" in the userID field */
function changeBoxUid()
{
document.getElementById('uid1').style.display='none';
document.getElementById('uid2').style.display='';
document.getElementById('uid').focus();
}
function restoreBoxUid()
{
if(document.getElementById('uid').value=='')
{
document.getElementById('uid1').style.display='';
document.getElementById('uid2').style.display='none';
}
}
/* End code for userID field */
/* Code for putting the visible word "Password" in the password field */
function changeBoxPwd()
{
document.getElementById('div1').style.display='none';
document.getElementById('div2').style.display='';
document.getElementById('pwd').focus();
}
function restoreBoxPwd()
{
if(document.getElementById('pwd').value=='')
{
document.getElementById('div1').style.display='';
document.getElementById('div2').style.display='none';
}
}
/* End code for password field */
</script><div id="loginBody">
<form target="_top" method="post" id="login" name="login" action="ePortal/ctrl" onsubmit="
if (locked) {
return false;
}
else
{
locked=true;
doLogin();
};
">
<div class="PcLoginPositionInput">
<input value="user" name="action" type="hidden"><input value="login" name="spec" type="hidden">
<div id="uid1">
<input onfocus="changeBoxUid()" name="uid_temp" type="text" class="PcLoginInputFields" value="Brukernavn">
</div>
<div id="uid2" style="display: none;">
<input onblur="restoreBoxUid()" value="" id="uid" name="uid" type="text" class="PcLoginInputFields">
</div>
<div id="div1">
<input onfocus="changeBoxPwd()" type="text" name="pass_temp" class="PcLoginInputFields" value="Passord">
</div>
<div style="display:none" id="div2">
<input onblur="restoreBoxPwd()" value="" type="password" id="pwd" name="pwd" class="PcLoginInputFields">
</div>
</div>
<div class="PcLoginForgottenPwd">
<a class="login" href="ePortal/ctrl?action=requestnewpassword">Glemt passord?</a>
</div>
<div id="PcLoginRegister"></div>
<input value="" type="submit" class="login_rollover">
</form>
</div>
</div>
我试过uid、uid1、uid2、uid\u temp、div1、div2、pass\u temp和pwd,但屏幕上没有任何操作
以下是我在网站上使用的代码:
Sub EMO_login()
' Source for this code:
' http://stackoverflow.com/questions/24038230/fill-user-name-and-password-in-a-webpage-using-vba
' http://stackoverflow.com/questions/25319983/logging-in-to-website-with-excel-vba
Set ie = CreateObject("InternetExplorer.application")
Bnavn = User1
Pw = testPass1
With ie
.Visible = True
.navigate "www.emo.no"
Do While .Busy Or _
.readyState <> 4
DoEvents
Loop
'This is where the input command should be.
Do While .Busy Or _
.readyState <> 4
DoEvents
Loop
End With
Set ie = Nothing
End Sub
我不知道在这里放什么。有人能告诉我填写这两个字段以及提交和登录的命令吗?谢谢@nhee帮助我找到解决方案 工作代码如下所示:
Sub EMO_login()
Dim ie As Object
Dim sht As Worksheet
Set sht = Sheet8
Set ie = CreateObject("InternetExplorer.application")
With ie
.Visible = True
.navigate "www.emo.no"
Do While .Busy Or _
.readyState <> 4
DoEvents
Loop
'Login procedure
ie.document.getElementById("uid2").getElementsByTagName("input")(0).Value = "MyUsername"
ie.document.getElementById("div2").getElementsByTagName("input")(0).Value = "MyPassword"
ie.document.forms("login").submit
Do While .Busy Or _
.readyState <> 4
DoEvents
Loop
End With
Set ie = Nothing
End Sub
我建议查看getElementById和getElementsByTagName。您可以使用ie.document.getElementByIduid2.getElementsByTagNameinput0.Value=…我仍然无法填写表单。您在哪一行遇到了什么错误?我没有遇到错误。宏运行正常,但窗体中没有可见数据。我试着在运行脚本后手动提交它,并且那里肯定有一个值,所以它似乎可以工作,但是我正在努力寻找提交表单的代码。请您也帮我一下好吗?试试ie.document.forms0.submit