使用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。例如,我在页面上有一个项目,它有一个onclick事件执行submitCommand('lookup'),所以我想直接在$ 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个span项)。
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))
$spans=@($ie.document.getElementsByTagName("SPAN"))
管道到对象以过滤您需要的基于其属性),然后调用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屋!