Automating JavaScript confirmation prompt in IE using PowerShell

我与影子孤独终老i 提交于 2019-12-25 18:41:37

问题


I have the following JavaScript code in an HTML page:

<script language="javascript" type="text/javascript">
    function confirmAutoTransitionSelection() {
        var autoTransition = 'OFF';
        if (document.getElementById('autoTransitionON').checked) {
            autoTransition = 'ON';
        }
        return confirm('Are you sure you want to turn ' + autoTransition + ' the Auto Transition Setting?');
        }
</script>

Also in the HTML code, I have the following input buttons that I'm auto-clicking with PowerShell script:

<form method="post">
Auto Transition
<input type="radio" id="autoTransitionON" name="autoTransition" checked value="true" /> ON
<input type="radio" name="autoTransition" value="false" /> OFF
<input class="submitBtn" type="submit" name="autoTransitionBtn" onclick="javascript:return confirmAutoTransitionSelection();" value="[Update]"/>
</form>

And here is my PowerShell script:

$ie = new-object -com "InternetExplorer.Application"
$ie.Navigate("http://localhost/test/")
While ($ie.Busy) {Sleep 2}
$doc = $ie.Document
$autobutton = $doc.getElementById("autoTransitionON").Click()
$btn = $doc.getElementsByTagName("input")
$autotransbtn = $btn | ? {$_.Name -eq "autoTransitionBtn"}
$autotransbtn.Click()
$ie.Quit()

I don't have any problems auto-clicking the input buttons with the PowerShell script, but the JavaScript is throwing a confirmation that has to be clicked to confirm the change. This is where I'm having a problem. How do I auto-click the JavaScript confirm?

Thanks in advance!


回答1:


I ran into a similar problem, and I basically queried the outerHTML property of the item to get the HTML code. Then all you have to do is replace the "confirm (*)" with "". After that you can set it back into the script and then click it.

$Script = $IE.document.getElementsByTagName("script").item(#----------Whatever Number it is)
$Script.outerHTML = $Script.outerHTML | % {if ($_ -like "return confirm*") {"return true;"} else {$_}}
$autotransbtn.Click()


来源:https://stackoverflow.com/questions/17686136/automating-javascript-confirmation-prompt-in-ie-using-powershell

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