1

我正在尝试运行一个 vbscript,它将执行以下操作:

  1. 启动 IE 并加载网站。
  2. 登录网站。
  3. 验证登录是否成功。

现在我得到了处理第 1 部分和第 2 部分的脚本。它的第 3 部分,即验证我坚持的登录成功。我该怎么做呢?

这是我从某个论坛上得到的代码:

 Dim IE
 Set IE = CreateObject("InternetExplorer.Application")
 IE.Visible = 1 
 IE.navigate "https://my.website.com"
 Do While (IE.Busy)
   WScript.Sleep 10
 Loop
 Set Helem = IE.document.getElementByID("formUsername")
 Helem.Value = "username" ' change this to yours
 Set Helem = IE.document.getElementByID("formPassword")
 Helem.Value = "password" ' change this to yours
 Set Helem = IE.document.Forms(0)
 Helem.Submit
4

2 回答 2

3

The best way to verify login success at this high a level is probably to search for an element in the HTML that only appears when logged in. If that exists assume you're logged in, if it doesn't try to navigate and log in again.

Here is a very simple example that uses Len() to check if an element containing text exists. You can be more sophisticated if you want and do things like verify that the information you're seeing matches what you would see if you were logged in.

You can use the same functions you used above to grab elements and then compare any of their members.

Dim IE
Dim Helem

Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = 1 
IE.navigate "http://www.example.com"

Set Helem = IE.document.getElementByID("formUsername")
Helem.Value = "username" ' change this to yours
Set Helem = IE.document.getElementByID("formPassword")
Helem.Value = "password" ' change this to yours
Set Helem = IE.document.Forms(0)
Helem.Submit

Do While (IE.Busy)
    WScript.Sleep 10
Loop

Dim someElement
Set someElement = IE.document.getElementByID("someElement")

If Len(someElement.innerText) > 0 Then
    MsgBox "logged in"
End If
于 2013-05-08T12:58:08.840 回答
1

There are a couple of things you can do

  • If the website returns a error message like "Login failed" or "Bad Password" you could check the body for that specific text

    If inStr(lcase(IE.document.body.innertext), "bad login") then
      'Do error checking here
    End If
  • If the title tag changes once you log in, you could also check for that to verify that you have been logged in.
于 2013-05-08T12:59:21.640 回答