目录
1.在已经打开的窗口中,找到百度的窗口(【百度一下,你就知道】)
■前言
从2022年6月15日起,
微软将终止IE浏览器服务支持 。
而VBS并不支持,Edge,Chrome等浏览器。
【IE不能使用后】使用Chrome插件,自定义JS插件操作浏览器
■举几个简单VBS操作IE的例子
1.在已经打开的窗口中,找到百度的窗口(【百度一下,你就知道】)
输入abc,
点击【百度一下】按钮
AutoSetting()
Function AutoSetting()
msgbox "111"
' get Operate window
Dim objPage
Set objPage = GetObj()
msgbox "222"
Dim entry
Set entry = objPage.document.getElementById("kw")
msgbox "333"
entry.Focus
entry.Value = "abc"
' when element no Id and Name , Use this
Dim objs
Set objs = objPage.document.getElementsByTagName("button")
If objs Is Nothing then
MsgBox "No Page Opened"
WScript.Quit
End IF
For Each obj In objs
If obj.innerText = "百度一下" Then
obj.Focus
obj.Click
'obj.Value
WScript.Sleep(70)
Set obj = Nothing
Exit For
End If
Next
End Function
Function GetObj()
Dim objWindow
Set objShell = CreateObject("Shell.Application")
For Each objWindow In objShell.Windows
If InStr(objWindow.LocationName,"百度一下,你就知道") Then
' Wait the page load completed
Do Until objWindow.document.ReadyState = "complete"
WScript.Sleep(70)
Loop
' set the return Object
Msgbox "Find"
Set GetObj= objWindow
' clear var
Set objShell = Nothing
Exit Function
End If
Next
' when Not Find
Msgbox "Not Find"
Set objShell = Nothing
Set GetObj= Nothing
End Function
---
其他获取元素操作
document.getElementById("XXXX").Value = "11111";
document.getElementsByName("commit")[0].Click;
document.forms[0].submit();
===
2.直接打开一个新的IE窗口
VBA:通过API函数,调用计算器,模拟按键等操作_小鸿1983的博客-优快云博客_vba的api
Public Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Public Declare PtrSafe Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
Public objectIE As Object
' ================================
Sub openIE()
' Dim objShell As Object
' Set objShell = CreateObject("Shell.Application")
Set objIE = CreateObject("InternetExplorer.Application")
' objIE.Visible = True
urlStr = "www.baidu.com"
objIE.navigate urlStr
' hWnd是,IE窗口句柄
ShowWindow objIE.hwnd, 3
Sleep 2000
' Do Some Thing XXXXXX
SendKeys "%{F4}", True
Sleep 1000
Set objIE = Nothing
End Sub
===
■如何让IE打开是IE,而不是Edge
选择从不
■其他更多VBS相关
・更多操作一览
・按键 shift ctrl alt
VBS操作 PDF时,常用快捷键(Adobe Acrobat Reader)_sun0322-优快云博客
键盘按键 | 对应的字符 |
Shift | + |
Ctrl | ^ |
Alt | % |
・打开图片,以幻灯片形式显示
blackScreen()
Function blackScreen()
Dim wshShell
Set wshShell = Wscript.CreateObject("Wscript.Shell")
wshShell.Run("C:\test\black.png")
WScript.Sleep 1000
wshShell.SendKeys "{F5}"
End Function
・操作截图软件
Dim wshShell
Set wshShell = Wscript.CreateObject("Wscript.Shell")
wshShell.Run("%windir%\system2\SnippingTool.exe")
WScript.Sleep 3000
‘获取画面
wshShell.Sendkeys "^+{N}"
‘或者 wshShell.Sendkeys "^{N}"
‘保存画面
wshShell.Sendkeys "^+{S}"
‘或者 wshShell.Sendkeys "^{S}"
・VBS调用VBA中的函数
如何使用VBS调用VBA函数_sun0322-优快云博客_vbs 调用vba
・VBS操作Excel数据
VBS操作Excel数据_sun0322-优快云博客_vbs读取excel数据
・关闭所有VBA程序
taskkill /f /im wscript.exe
・xxx
■使用Chrome插件,自定义JS插件操作浏览器
1.代码
manifest.json
{
"name": "第一个Chrome插件",
"manifest_version": 3,
"version": "1.0",
"description": "我的第一个Chrome插件,sss gogo!",
"content_scripts": [
{
"matches": ["https://www.baidu.com/"],
"js": ["myscript.js"]
}
]
}
myscript.js
test002();
function test002() {
alert("HelloWorld Begin");
var myVar = '11111';
// alert(myVar );
// 以下代码没有效果
// document.getElementById("kw").value="aaa"
document.querySelector("#kw").value="123";
// document.forms[0].submit();
alert("End");
}
代码注意:
document.querySelector("#kw").value="123";
・value必须是小写,不能是大写 比如 Value
・结尾的分号有没有都可以
・在Edge上,也可以使用
2.导入自定插件
导入HelloWord文件夹(浏览器 URL 直接输入下面内容)
chrome://extensions
选择【加载已解压的扩展程序】 选择你存放上面两个文件的【文件夹】【helloWord】
页面加载时,你的JS便会被执行
--
3.获取画面项目的JS URL
在【开发者工具】的【Elements】中,选择对应的元素,右键【...】
选择【Copy】选择【Copy JS path】
得到的值如下
document.querySelector("#kw")
在插件中,我们使用上面的代码,就可以对这个元素进行操作了。
---
4.关于【manifest.json】的更多介绍
chrome扩展:manifest.json文件详解_超频化石鱼的博客-优快云博客_chrome manifest.json
---