使用 PowerShell 通过 Internet Explorer 的 com 界面执行 javascript

发布于 2024-08-05 07:27:37 字数 589 浏览 1 评论 0原文

我正在使用 PowerShell 编写一些 Internet Explorer 自动化脚本。以下是我启动 IE com 对象的方法:

$ie = New-Object -com "InternetExplorer.Application"
$ie.Navigate("about:blank")
$ie.visible = $true

$doc = $ie.Document

因此,我想做的是在 $doc 对象上执行一些 javascript。例如,我的页面上有一个项目有一个执行 submitCommand('lookup')onclick 事件,因此我想直接在 onclick 上运行该事件。 code>$doc 而不必在页面上查找对象,然后调用其上的 Click() 方法。

这会更容易,因为该对象没有名称也没有 id,这使得更改非常明智,因为我只能依赖它在页面上的位置(例如:页面上的第 11 个跨度项目)。

或者,您将如何根据元素的类别来选择元素?这会很有帮助,因为“按钮”有它自己的类。

谢谢

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

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.

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.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

美男兮 2024-08-12 07:27:37

$spans=@($ie.document.getElementsByTagName("SPAN"))

管道到 where-object 以过滤您需要的对象(基于其属性),然后调用 click 方法,例如:

$span11 = $spans | where {$_.innerText -eq 'something'}
$span11.click()

$spans=@($ie.document.getElementsByTagName("SPAN"))

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()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文