bea*_*man 0 browser vbscript dom
我使用自动化脚本来测试基于浏览器的应用程序.我想将我加载的每个页面的可见文本保存为文本文件.这需要适用于当前打开的浏览器窗口.我遇到过一些使用的解决方案,InternetExplorer.Application但这对我不起作用,因为它必须是当前的打开页面.
理想情况下,我想使用vbscript实现这一目标.任何想法如何做到这一点?
您可以附加到已经运行的IE实例,如下所示:
Set app = CreateObject("Shell.Application")
For Each window In app.Windows()
If InStr(1, window.FullName, "iexplore", vbTextCompare) > 0 Then
Set ie = window
Exit For
End If
Next
Run Code Online (Sandbox Code Playgroud)
然后保存文档正文文本,如下所示:
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.OpenTextFile("output.txt", 2, True)
f.Write ie.document.body.innerText
f.Close
Run Code Online (Sandbox Code Playgroud)
如果页面包含非ASCII字符,则可能需要使用Unicode编码创建输出文件:
Set f = fso.OpenTextFile("output.txt", 2, True, -1)
Run Code Online (Sandbox Code Playgroud)
或将其保存为UTF-8:
Set stream = CreateObject("ADODB.Stream")
stream.Open
stream.Type = 2 'text
stream.Position = 0
stream.Charset = "utf-8"
stream.WriteText ie.document.body.innerText
stream.SaveToFile "output.txt", 2
stream.Close
Run Code Online (Sandbox Code Playgroud)
编辑:这样的东西可能有助于摆脱文档正文中的脚本代码:
Set re = New RegExp
re.Pattern = "<script[\s\S]*?</script>"
re.IgnoreCase = True
re.Global = True
ie.document.body.innerHtml = re.Replace(ie.document.body.innerHtml, "")
WScript.Echo ie.document.body.innerText
Run Code Online (Sandbox Code Playgroud)