Execute javascript trough Internet Explorer's com interface using PowerShell

≡放荡痞女 提交于 2019-11-27 16:42:41

问题


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


回答1:


$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()


来源:https://stackoverflow.com/questions/1444330/execute-javascript-trough-internet-explorers-com-interface-using-powershell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!