用于自动网站登录测试的 Vbscript [英] Vbscript for automated website login testing
问题描述
我正在尝试运行一个 vbscript,它将执行以下操作:
I am trying to run a vbscript, that would do following:
- 启动 IE 并加载网站.
- 登录网站.
- 验证登录是否成功.
现在我得到了处理第 1 部分和第 2 部分的脚本.它的第 3 部分,即验证我遇到的登录成功.我该怎么做?
Now I have got the script that deals with part 1 and 2. Its part 3, i.e. verify login success that I am stuck with. How do I do this?
这是我从某个论坛得到的代码:
Here is the code I got off some forum:
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
推荐答案
在这种高级别验证登录成功的最佳方法可能是在 HTML 中搜索仅在登录时出现的元素.如果存在,假设您已登录,如果它没有尝试导航并再次登录.
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.
这是一个非常简单的示例,它使用 Len() 检查包含文本的元素是否存在.如果您愿意,您可以更复杂一些,例如验证您看到的信息是否与您登录后看到的信息相符.
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
这篇关于用于自动网站登录测试的 Vbscript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!