login到网站

我试图创build一个Excelmacros,将我login到这个网站 。 我写了macros来login到其他网站,但这是不同的。

当我使用以下代码以编程方式插入用户名和密码,然后以编程方式单击“login”button时,网页带有消息

错误:请提供用户名和错误:请提供密码

Sub LogIn() ' Open IE and navigate to the log-in page Set ie = CreateObject("InternetExplorer.Application") With ie .Visible = True .navigate "https://www.prudential.com/login" ' Loop until the page is fully loaded Do Until Not ie.Busy And ie.ReadyState = 4 DoEvents Loop Application.Wait (Now + TimeValue("0:00:10")) ' Enter the necessary information on the Login web page and click the submit button ie.document.getElementById("username").Value = "abcdef" ie.document.getElementById("password").Value = "12345678" ie.document.getElementsByClassName("btn btn-primary btn-login btn-sm-block analytics-login")(0).Click ' Loop until the page is fully loaded Do Until Not ie.Busy And ie.ReadyState = 4 DoEvents Loop End With ' Do stuff ' Quit IE ie.Quit End Sub 

我可以看到我的代码已经将用户名和密码插入到网页框中,但是由于某些原因,在成功单击“提交”button之后,用户名和密码未被检测到。

什么代码将成功地插入用户名和密码,以便它可以被网页处理? 当您收到以下错误消息时,您将知道您的代码正常工作:

我们无法validation您的用户名和密码。 请再试一次。

谢谢你的帮助!

它实际上希望你发送一个密钥,所以你可以绕过这样的请求:

 ie.document.getElementById("username").Focus ie.document.getElementById("username").Value = "abcde" Application.SendKeys "f", True ie.document.getElementById("password").Focus ie.document.getElementById("password").Value = "1234567" Application.SendKeys "8", True ie.document.getElementsByClassName("btn btn-primary btn-login btn-sm-block analytics-login")(0).Click 

编辑:

 Sub GetData() Dim ie As InternetExplorer Dim desc As IHTMLElement Set ie = New InternetExplorer With ie .navigate "https://www.prudential.com/login" .Visible = True End With Do While (ie.Busy Or ie.ReadyState <> READYSTATE_COMPLETE) DoEvents Loop ie.document.getElementById("username").Focus Application.SendKeys "abcdef" Application.Wait (Now + TimeValue("0:00:01")) ie.document.getElementById("password").Focus Application.SendKeys "12345678" Application.Wait (Now + TimeValue("0:00:01")) ie.document.getElementsByClassName("btn btn-primary btn-login btn-sm-block analytics-login")(0).Click Set ie = Nothing End Sub 

试试下面的代码:

 Option Explicit Const READYSTATE_COMPLETE As Long = 4 Const URL_String As String = "https://www.prudential.com/login" Sub Login() Dim IE As Object Dim ObjElement As Object ' using Late Binding Set IE = CreateObject("InternetExplorer.Application") With IE .navigate URL_String .Visible = True End With Do While (IE.Busy Or IE.ReadyState <> READYSTATE_COMPLETE) DoEvents Loop IE.document.getElementByid("username").Value = "abcdef" IE.document.getElementByid("password").Value = "12345678" ' Optional: I didn't need the wait time Application.Wait (Now + TimeValue("0:00:01")) Set ObjElement = IE.document.getElementsByClassName("btn btn-primary btn-login btn-sm-block analytics-login") ObjElement(0).Click Set IE = Nothing End Sub 

如果其他人有类似的问题,我终于能够得到Sendkeys的工作。 不过,如果任何人都可以拿出一个非SendKey解决scheme,那么这将是非常受欢迎的。

获得Sendkey工作的两个要点是1)使用

  Set WSS = CreateObject("WScript.Shell") 

2)确保从Excel( 例如 tools-macro-macros)运行macros,而不是从VB编辑器运行(从编辑器运行将简单地插入代码内的文本,而不是在目标网页上)。

 ' Open IE and navigate to the log-in page Application.StatusBar = "Opening IE and logging in to the website" Set WSS = CreateObject("WScript.Shell") my_username = "abcdef" my_pw = "12345678" Set ie = CreateObject("InternetExplorer.Application") With ie .Visible = True .navigate "https://www.prudential.com/login" .Top = 0 .Left = 0 .Height = 1025 .Width = 1925 ' Loop until the page is fully loaded Do Until Not ie.Busy And ie.ReadyState = 4 DoEvents Loop Application.Wait (Now + TimeValue("0:00:10")) WSS.SendKeys "{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}" Application.Wait (Now + TimeValue("0:00:01")) WSS.SendKeys my_username Application.Wait (Now + TimeValue("0:00:01")) WSS.SendKeys "{TAB}" Application.Wait (Now + TimeValue("0:00:01")) WSS.SendKeys my_pw Application.Wait (Now + TimeValue("0:00:01") ie.document.getElementsByClassName("btn btn-primary btn-login btn-sm-block analytics-login")(0).Click ' Loop until the page is fully loaded Do Until Not ie.Busy And ie.ReadyState = 4 DoEvents Loop End With ' You're now logged in, do your stuff..