2016-01-26 41 views
0

我在通過Web Browser向表單發送值時出現問題。我的目標是發佈一個帖子,或者send這些inputsdo表單的值,並且submit與在我的browser中打開生成的頁面後相同。使用WebBrowser處理頁面

表:

<form action="..." method="post" accept-charset="ISO-8859-1" name="postmodify" id="postmodify" class="flow_hidden" onsubmit="submitonce(this);smc_saveEntities('postmodify', ['subject', 'message', 'guestname', 'evtitle', 'question'], 'options');" enctype="multipart/form-data"> 

    <input type="text" name="subject" tabindex="1" size="80" maxlength="80" class="input_text" /> 


    <select name="icon" id="icon" onchange="showimage()"> 
     <option value="xx" selected="selected">Default</option> 
     <option value="thumbup">OK</option> 
     <option value="thumbdown">Down</option> 
     <option value="exclamation">Exclamation</option> 
     <option value="question">question</option> 
     <option value="lamp">lamp</option> 
     <option value="smiley">smiley</option> 
     <option value="angry">angry</option> 
     <option value="cheesy">cheesy</option> 
     <option value="grin">grin</option> 
     <option value="sad">sad</option> 
     <option value="wink">wink</option> 
    </select> 

    <textarea class="resizeble" name="message" id="message" rows="12" cols="600" onselect="storeCaret(this);" onclick="storeCaret(this);" onkeyup="storeCaret(this);" onchange="storeCaret(this);" tabindex="2" style="height: 175px; width: 100%; "></textarea> 

    <input type="submit" value="Send" tabindex="3" onclick="return submitThisOnce(this);" accesskey="s" class="button_submit" /> 

</form> 

C#:

WebBrowser oWebBrowser = new WebBrowser(); 
oWebBrowser.ScriptErrorsSuppressed = true; 
oWebBrowser.Navigate("myLinkinInternet"); 

//button to posting 
private void post_Click(object sender, EventArgs e) 
{ 
    try 
    { 
     HtmlElement subject = oWebBrowser.Document.GetElementsByTagName("input")["subject"]; 
     if(subject != null) 
     { //attempt to set the subject 
      subject.SetAttribute("value", subj); 
      MessageBox.Show("subj"); 
     } 

     HtmlElement ico = oWebBrowser.Document.GetElementById("icon"); 
     if (ico != null) 
     { //attempt to set the icon 
      ico.SetAttribute("value", m.traduzIcon(icon.Text)); 
      MessageBox.Show("icon"); 
     } 


     HtmlElement message = oWebBrowser.Document.GetElementById("message"); 
     if (message != null) 
     { //attempt to set the message 
      message.InnerText = default; 
      MessageBox.Show("default"); 
     } 


     HtmlElement form = oWebBrowser.Document.GetElementById("postmodify"); 
     if (form != null) 
     { //attempt to submit 
      form.InvokeMember("submit"); 
      MessageBox.Show("submit"); 
     } 
     //attempt to open the link poster 
     ProcessStartInfo post = new ProcessStartInfo(oWebBrowser.Url.AbsoluteUri); 
     Process.Start(post); 

    } 
    catch 
    { 
     MessageBox.Show("ERRO"); 
    } 
} 

當運行不顯示任何MessageBox並打開Web Browser定義我的瀏覽器頁面(myLinkInInternet - 一個與表單) 。任何人都可以給我一個幫助嗎?我不知道該怎麼做,我正在研究。

回答

0

而不是與DOM戰鬥,我通常使用WebBrowser.InvokeScript方法有更好的運氣。例如,如果可以的話,腳本標籤具有相似內容添加到頁面

<script> 
function PostForm(subj) { 
    document.getElementsByName('subject').value = subj; 
    document.getElementById('postmodify').submit(); 
} 
</script> 

你應該能夠通過

oWebBrowser.InvokeScript("PostForm", subj); 

調用它如果不能注入的JavaScript進入源代碼,也許你可以使用類似於similar SO question發佈的代碼動態添加它。

如果你想獲取方法的工作,重要的是理解控制的比名爲value屬性截然不同是很重要的。該屬性用於初始化控件的值,但不代表瞬時值。也許DOM元素有一個Value屬性?

注意:我還沒有測試過這些,所以很可能是我的代碼或思考中的錯誤。