简单的问题。我使用的是这样的:
$links = @("example.com", "example.net", "example.org")
$IE = new-object -com internetexplorer.application
$IE.visible = $true
for($i = 0;$i -lt $links.Count;$i++) {
$find = $links[$i]
$IE.navigate2($find)
}
我想在环路中类似$IE.addscript("//MyJavaScriptCode")
插入JavaScript代码页面上的控制台(或只是使其运行)。
我怎么说任务是什么?
谢谢!
让我们来谈谈添加脚本。
第一件事情是不存在等待在COM事件。当你运行一个应用程序的运行(在这种情况下,IE)将运行的动作,所以PowerShell有没有办法知道是否完成了动作。
在这种情况下,让我们来谈谈导航。一旦运行了命令,你需要找到除了爱情之外,等待,然后进行下一步的资产净值来完成。
幸运我们拥有财产的readyState。 $IE.Document.ReadyState
我们需要找到除了爱情之外等待的readyState等于完成
While($IE.Document.readyState -ne 'Complete'){
sleep -Seconds 1
}
现在,它的时间来添加脚本。有没有直接的方法将脚本添加到脚本。因此,我们可以通过运行JavaScript来添加脚本解决这一点。 $IE.Document.Script.execScript(Script Here, Script Type)
我们可以在JavaScript中创建一个新的元素和追加该元素的头。在这种情况下要使用到谷歌的jQuery的库
var Script = document.createElement('script');
Script.setAttribute('src','https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js');
document.head.appendChild(Script);
现在,一旦我们添加脚本,我们需要等待IE添加到页面,所以我们需要一个短暂的延迟脚本。在这种情况下,我做了1秒。
我跑了测试,以确保通过检查jQuery的版本加载alert($.fn.jquery);
加载脚本
$JS = @'
var Script = document.createElement('script');
Script.setAttribute('src','https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js');
document.head.appendChild(Script);
'@
$GetVersion = @'
alert($.fn.jquery);
'@
$links = @("google.com")
$IE = new-object -com internetexplorer.application
$IE.visible = $true
$links | %{
$Document = $IE.navigate2($_)
While($IE.Document.readyState -ne 'Complete'){
sleep -Seconds 1
}
$IE.Document.body.getElementsByTagName('body')
$TEST = $IE.Document.Script.execScript($JS,'javascript')
sleep -Seconds 1
$IE.Document.Script.execScript($GetVersion,'javascript')
}