2011-05-12 26 views
4

我有一個PowerShell腳本,我想運行一個HTML表單。我所擁有的只是一些表單域和一個按鈕。當我運行我的powershell腳本時,它會打開一個新的ie窗口,然後用窗體導航到正確的頁面。如何在用戶單擊按鈕後收集填寫在表單中的信息?使用HTML表單作爲PowerShell的GUI

編輯:

下面是一些代碼,我試圖讓工作:

function onClick($server) 
{ 
    $server.value="here" 
} 

$ie = new-object -com "Internetexplorer.Application" 
$ie.navigate("bulk_upload.html") 
$ie.visible = $true 

$doc = $ie.document 
$btn = $doc.getElementById("submit") 
$server = $doc.getElementById("server") 

$btn.add_onclick({onClick $server}) 

我運行此代碼,我點擊按鈕後沒有任何反應

UPDATE我試過這個代碼:

$ie = new-object -com "Internetexplorer.Application" 
$ie.navigate("bulk_upload.html") 
$ie.visible = $true 

$doc = $ie.document 
$btn = $doc.getElementById("submit") 

$eventId = Register-ObjectEvent $btn HTMLButtonElementEvents_Event_onclick -Action {write-host 'hi'} 

而且我得到這個錯誤:

不能註冊事件。不支持需要返回值的事件

回答

2

以下關於使用PowerShell進行Web UI自動化的文章可能會對您有所幫助。 http://msdn.microsoft.com/en-us/magazine/cc337896.aspx

+0

的EXECSCRIPT(「SCRIPT HERE」,「類型的腳本」)添加JavaScript,但它不是我所期待的。這使Web表單自動化。我不想自動化它,我想抓住用戶輸入的內容到 – Steve 2011-05-12 18:33:26

+0

您可以獲得輸入,這不是問題。更具挑戰性的是如何確定「何時去做」。 com對象(InternetExplorer.Automation)上有一些事件可能會有所幫助。 – stej 2011-05-12 21:14:57

+0

我一直試圖使用這些事件沒有運氣。我還注意到有一種叫做add_onclick的方法。我試圖將其添加到我的按鈕,並沒有任何反應。我改變了我的問題,以顯示這個 – Steve 2011-05-13 15:52:12

0

因爲您已經能夠從PowerShell腳本中打開一個新的ie窗口並導航到正確的頁面,所以我假設您使用的是InternetExplorer Object。這就是爲什麼我認爲@ravikanth鏈接對你有好處。從那裏(以及互聯網上的許多其他帖子),你可以看到如何使用$ie.Document.getElementById("fieldid").value來獲取價值領域,是嗎?

另一個選項是System.Net.WebClient這是GET/POST網頁數據的正確方式。你有一個很好的例子here

否則,我認爲你應該更清楚你想做什麼,並提供你正在嘗試做的一些代碼示例。

+0

我添加了代碼到我的問題 – Steve 2011-05-13 16:23:19

3

我試圖完成同樣的事情,在你的後跑了。我看到它已經有8個月了,但如果你仍然在這裏看到我發現的東西。我無法使用「onclick」工作。所以我使用了一個隱藏的輸入字段,併爲該按鈕定義了一個動作來更新該字段的值。在PowerShell中,我爲隱藏字段獲取ElementbyId,並使用它來識別用戶已完成輸入。

$html = @" 
<html> 
<body> 
<form> 
First name: <input type="text" id="firstname"> 
<br> 
Last name: <input type="text" id="lastname"> 
<br> 
<input type="hidden" name="finished" value="0"> 
<input type="button" id="button" value="Continue" onclick="finished.value=1"> 
</form> 
</body> 
</html> 
"@ 

$ie = new-object -com "InternetExplorer.Application" 
$ie.navigate2("about:blank") 
$ie.AddressBar = $false 
$ie.MenuBar  = $false 
$ie.StatusBar = $false 

$ie.document.body.innerHTML = $html 
$ie.Visible = $true 
$doc = $ie.document 

while ($doc.getElementByID("finished").value -ne 1) { 
    start-sleep -m 500 
} 

$ie.Visible = $false 
"You answered:" 
$doc.getElementByID("firstname").value 
$doc.getElementByID("lastname").value 
$ie.quit() 
0

我用我看到一個父窗口

$html = @" 
<html> 
    <body> 
     <form> 
       First name: <input type="text" id="firstname"> 
      <br> 
       Last name: <input type="text" id="lastname"> 
      <br> 
      <input type="button" id="finished" value="Submit" onclick=''> 
      </form> 
    </body> 
</html> 
"@ 

$ie = new-object -com "InternetExplorer.Application" 
$ie.navigate("about:blank") 
$ie.document.body.innerHTML = $html 
$doc = $ie.Document 
$doc.parentWindow.execScript("document.getElementById('finished').onclick = function(){document.getElementById('finished').value = 'Submitted';};","javascript") 
$ie.Visible = $true 

while ($doc.getElementByID("finished").value -ne "Submitted") { 
    start-sleep -m 500 
    $firstName = $doc.getElementByID("firstname").value 
    $lastName = $doc.getElementByID("lastname").value 
} 

$ie.Visible = $false 
"You answered:" 
$doc.getElementByID("firstname").value 
$doc.getElementByID("lastname").value 
$ie.quit()