使用 PowerShell 通过 Internet Explorer 的 com 接口执行 javascript [英] Execute javascript trough Internet Explorer's com interface using PowerShell
问题描述
我正在使用 PowerShell 编写一些 Internet Explorer 自动化脚本.下面是我如何启动 IE com 对象:
I am writing some Internet Explorer automation scripts using PowerShell. Here is how I start the IE com object:
$ie = New-Object -com "InternetExplorer.Application"
$ie.Navigate("about:blank")
$ie.visible = $true
$doc = $ie.Document
所以,我想做的是在 $doc
对象上执行一些 javascript.例如,我在页面上有一个项目,它有一个执行 submitCommand('lookup')
的 onclick
事件,所以我想直接在 $doc
而不是必须在页面上找到对象,然后在其上调用 Click()
方法.
So, what I would like to do is to execute some javascript on the $doc
object. For example, I have an item on the page that has an onclick
event which executes submitCommand('lookup')
, so I'd like to run that directly on the $doc
instead of having to find the object on the page and then calling the Click()
method on it.
这会更容易,因为对象没有名称或 ID,因此更改非常明智,因为我只能依赖它在页面上的位置(例如:页面上的第 11 个跨度项目).
It would be easier as the object has no name nor id, making it very sensible to change as I can only rely on it's position on the page (eg: the 11th span item on the page).
另外,您将如何根据类选择元素?这会很有帮助,因为按钮"有它自己的类.
Alternatively, how would you select elements based on their class? That would help a lot as the "button" has it's own class.
谢谢
推荐答案
$spans=@($ie.document.getElementsByTagName("SPAN"))
管道到where-object来过滤你需要的那个(根据它的属性)然后调用click方法,例如:
Pipe to where-object to filter the one you need (based on its attributes) and then call the click method, for example:
$span11 = $spans | where {$_.innerText -eq 'something'}
$span11.click()
这篇关于使用 PowerShell 通过 Internet Explorer 的 com 接口执行 javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!