如何在网页上输入用户名和密码?

时间:2016-06-07 13:41:51

标签: excel vba excel-vba

Q1)如何在此网页上输入密码和用户名" https://.com/1/19/login.esp" ?

我已经编写了访问网页的代码:

Sub login()
  Dim IE As Object

  Const navOpenInNewTab = &H800
  Set IE = CreateObject("InternetExplorer.Application")
  IE.Visible = True
  IE.Navigate "https://esp"
End Sub

Q2)我应该使用哪个createobject来访问Google Chrome?

Thnx提前

2 个答案:

答案 0 :(得分:1)

Sub login()
  Dim IE As Object
  Dim HTMLDoc As Object
  Dim objCollection As Object

  Const navOpenInNewTab = &H800
  Set IE = CreateObject("InternetExplorer.Application")
  IE.Visible = True
  IE.Navigate "https://ogin.esp"

  Do While IE.Busy Or IE.ReadyState <> 4: Loop

  Set HTMLDoc = IE.document

  With HTMLDoc
  HTMLDoc.getElementById("USERNAME").Value = "yyyy"
  HTMLDoc.getElementById("PASSWORD").Value = "xxxxx"
  End With


  Set objCollection = IE.document.getElementById("loginbutton")
  objCollection.Click

End Sub

此代码有效....

答案 1 :(得分:0)

要在网页上查找要通过VBA选择的项目,您需要按F12进入网页上的开发人员工具。

在那里,您可以找到要通过&#34进入的元素;进入&#34;页面上的代码,并选择您希望VAB选择的项目。

以下是如何做到的:

Click this to step into page code

然后选择用户名框:

enter image description here

选择此框后,开发人员框中的代码将突出显示,告诉您链接到所按项目的代码。

以下是用户名框的现在突出显示的代码:

enter image description here

我们有三个选项可以选择页面上的项目:

  • IE.document.getElementById(在代码中找到ID =,此处为id =&#39; USERNAME&#39;)
  • IE.document.getElementsByName(在代码中找到Name =,此处名称=&#39; USERNAME&#39;)
  • IE.document.getElementsByclassname(在代码中找到class =。在这种情况下,class =&#39; focus&#39;)

*注意:首先尝试使用ID,然后使用name,然后使用类,因为页面上只有1个元素将具有该1个ID,但是多个项目可以具有相同的名称或类别,或者保证其可以正常工作你有正确的身份证。

Dim IE As Object ' InternetExplorer.Application
Dim UserN As Object 'username field
Dim PW As Object 'password field
Dim LoginButton As Object 'Login button

'enter username and password in textboxes
Set UserN = IE.document.getElementByID("USERNAME")
'fill in first element named "username", assumed to be the login name field
UserN(0).Value = ""

Set PW = IE.document.getElementsByName("PASSWORD")
'fill in first element named "password", assumed to be the password field
PW(0).Value = ""

然后你需要找到页面上的按钮,然后单击它。 以下是一些代码:

'After entering in the user/pass, we need to click the button.
Dim objCollection As Object
Set objCollection = IE.document.getElementById("loginbutton")
objCollection.Click

有更多信息和更多关于如何正确使用每种选择方法的代码,所以这只是让你入门的准系统。 正在搜索VBA IE自动化&#39;能为你带来一些好的结果。

相关问题