0

I'm having problems sending values to a form via Web Browser. My goal is to make a post, or send the values for the inputsdo form and do submit the same after that open the generated page in my browser.

Form:

<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");
    }
}

When running it displays no MessageBox and open my browser page defined in the Web Browser (myLinkInInternet- the one with the form). Can anyone give me a help? I do not know how to do this right, I am basing myself on research.

Leonardo
  • 237
  • 1
  • 10

1 Answers1

0

Instead of fighting with the DOM, I have generally had better luck using the WebBrowser.InvokeScript method. For example, if you can, add a script tag to the page with content similar to

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

You should be able to call it via

oWebBrowser.InvokeScript("PostForm", subj);

If you can't inject the Javascript into the source code directly, perhaps you could add it dynamically using code similar to that posted for a similar SO question.

If you do want to get your method working, it is important to understand that the value of a control is very different than the attribute with the name value. This attribute is used to initialize the control's value, but does not represent the instantaneous value. Perhaps there is a Value property of the DOM element?

Note: I haven't tested any of this, so there very well could be errors in my code or thinking.

Community
  • 1
  • 1
erdomke
  • 4,980
  • 1
  • 24
  • 30