简体   繁体   English

在网页上的登录表单中输入用户名和密码

[英]Enter username and password in login form on a webpage

Using Excel VBA, I'm trying to login to a website.使用 Excel VBA,我正在尝试登录网站。

This is my code:这是我的代码:

Dim HTMLDoc As HTMLDocument
Dim oBrowser As InternetExplorer
Sub Website_Login()

Dim oHTML_Element As IHTMLElement
Dim sURL As String

sURL = "https://www.domain.com/login.html"

Set oBrowser = New InternetExplorer
'oBrowser.Silent = True
'oBrowser.timeout = 60
oBrowser.navigate sURL
oBrowser.Visible = True

Do
Loop Until oBrowser.ReadyState = READYSTATE_COMPLETE

Set HTMLDoc = oBrowser.Document

With HTMLDoc
  HTMLDoc.getElementById("username").Value = "user"
  HTMLDoc.getElementById("password").Value = "password"
End With

For Each oHTML_Element In HTMLDoc.getElementsByTagName("input")
  oHTML_Element.Click
  Exit For
Next

oBrowser.Quit

End Sub

The Javascript instructions in this code are working.此代码中的 Javascript 指令正在运行。 If I go to the page and enter them, they work.如果我转到页面并输入它们,它们就会起作用。 But when I run the code, IE opens but it doesn't enter the user/pass and click the button.但是当我运行代码时,IE 打开但它没有输入用户/密码并单击按钮。

Any help, please?请问有什么帮助吗?

I just tested this code and it worked.我刚刚测试了这段代码,它起作用了。

The only difference is that I used Late Binding (too lazy to set all references) and I added While .Busy to the Loop to wait for the URL to load.唯一的区别是我使用了Late Binding (懒得设置所有引用)并且我在循环中添加了While .Busy以等待 URL 加载。 I also changed input to button in the button click loop.我还在按钮单击循环中将input更改为button

Dim HTMLDoc As Object 'HTMLDocument
Dim oBrowser As Object 'InternetExplorer

Sub Website_Login()

Dim oHTML_Element As Object 'IHTMLElement
Dim sURL As String

sURL = "https://www.mypage.com/login.html"
Set oBrowser = CreateObject("InternetExplorer.Application") 'New InternetExplorer
'oBrowser.Silent = True
'oBrowser.timeout = 60
oBrowser.navigate sURL
oBrowser.Visible = True

With oBrowser
    Do While .Busy Or .ReadyState <> 4: Loop
End With


Set HTMLDoc = oBrowser.Document

With HTMLDoc
  HTMLDoc.getElementById("username").Value = "user"
  HTMLDoc.getElementById("password").Value = "password"
End With

For Each oHTML_Element In HTMLDoc.getElementsByTagName("button")
  If oHTML_Element.Name = "Submit" Then oHTML_Element.Click
  Exit For
Next

oBrowser.Quit

End Sub

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在登录表单(jQuery)中检查用户名和密码 - Check username and password in Login Form (jQuery) 检查用户名、密码 - 使用 React Hooks 的登录表单 - Check username, password - Login Form using React Hooks 登录将我带到相同的登录表单,并且在输入正确的用户名和密码时无法登录 - Login takes me to the same login form and does not login when entered the correct username and password 使用Javascript自动填充网页用户名密码 - Auto fill webpage username password using Javascript 如何制作一个登录表单,其中密码是输入的用户名输入的相反形式 - How to make a login form where the password is the reversed form of the typed in username input 填写用户名和密码后,当我按Enter键无法登录时,即google chrome工作正常。 怎么会? - after filling username and password when i press an enter button unable to login in ie but google chrome is working fine. how come? 如何使用 if else 语句从“登录成功”的登录表单中调用我的用户名和密码? 登录尝试 - How to call my username and password from my login form which is "login succesfully" using if else statement? For login attempt 用JavaScript制作的用户名和密码表格 - Username and password form made with JavaScript UIWebView将用户名和密码传递到表单中 - UIWebView passing username and password into a form 提交用户名和密码javascript(不是表格) - Submit username and password javascript (not a form)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM