Click here to Skip to main content
15,891,375 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
In a C# winform project I am using a webbrowser object to log into a website, navigate to a particular list and then work with that list. After logging in I need to select from some dropdown selection choices that are inside a form and then submit the form (there is no submit element - submitting the form is done by clicking on an image anchored with an onclick event, all as shown below.

Here's an abbreviated version of the code at the target site (I've shortened it quite a bit - i think I left everything pertinent in. I've also cut out all the styles to focus on the HTML and javascript). Here's the code:

<form name="frm1" id="frm1" action="/tab/formaction.asp" method="post">
<table>
<tr><td> Locations</td></tr>
<tr><td>
<table>
    <tr>
        <td>Origin</td>
        <td>
            <select title="Choose Origin Location(s)" name="origin_location"
             id="origin_location" multiple="multiple">
                <option value="0">ANYWHERE</option>
        <option value="1">North West</option>
        <option value="2">North Central</option>
        <option value="3">Great Lakes</option>
        <option value="4">North East</option>
        <option value="5">South West</option>
    </select>
        </td>
    </tr>
    <tr>
        <td>Destination</td>
        <td>
            <select title="Choose Destination Location(s)"
              name="destination_location" id="destination_location"
               multiple="multiple">
                <option value="0">ANYWHERE</option>
        <option value="1">North West</option>
        <option value="2">North Central</option>
        <option value="3">Great Lakes</option>
        <option value="4">North East</option>
        <option value="5">South West</option>
    </select>
        </td>
    </tr>

    <tr>
        <td>Number of People</td>
        <td>
            <select name="people">
        <option value="1">1+</option>
                    <option value="2">2+</option>
                    <option value="3">3+</option>
                    <option value="4">4+</option>
            </select>
            <select style="visibility:hidden;" name="equality">
                <!--
                <option value="<=">Less than or Equal</option>
                <option value="=">Equal</option>
                !-->
                <option value=">=">Greater than or Equal</option>
            </select>
        </td>
    </tr>
    <tr>
        <td>
            <a href="javascript: void 0;" onclick="objForm.submit(); return
                 false;">
                <img src="/tab/images/buttons/submit.gif" border="0"
                    WIDTH="71" HEIGHT="21">
            </a>
        </td>
    </tr>
</table>
</td></tr></table>
<input type="hidden" name="origin_text" id="origin_text">
<input type="hidden" name="origin_region_id" id="origin_region_id">
<input type="hidden" name="origin_state_id" id="origin_state_id">
<input type="hidden" name="origin_city_id" id="origin_city_id">

<input type="hidden" name="destination_text" id="destination_text">
<input type="hidden" name="destination_region_id"
      id="destination_region_id">
<input type="hidden" name="destination_state_id" id="destination_state_id">
<input type="hidden" name="destination_city_id" id="destination_city_id">

<input type="hidden" name="equality_text" id="equality_text">
<input type="hidden" name="sZip" id="sZip">
</form>


What I have tried:

My first thought was to set attributes for the first and second of the three things I want to choose, like this:

 <pre>webBrowser1.Document.GetElementById("origin_location")
   .SetAttribute("value", "4");

 webBrowser1.Document.GetElementById("destination_location")
    .SetAttribute("value", "4");


I am not sure that I am doing those 2 correctly, and for the third, there is no ID, so I have no idea how to select it.

Finally, I want to click the anchored image or to invoke the onclick script in the cell that reads:

<a href="javascript: void 0;" onclick="objForm.submit(); return false;">
    <img src="/tab/images/buttons/submit.gif" border="0" WIDTH="71"
     HEIGHT="21">
</a>

As might be obvious, I am a fairly new coder and appreciate any help or guidance.
Posted
Updated 23-Jan-17 12:34pm
v2

1 solution

You don't need to select controls. You simply need to know the proper url/querystring format/contents, and submit that with the WebClient object.
 
Share this answer
 
Comments
Ken-in-California 23-Jan-17 18:49pm    
John - thanks for your reply. I have some previous experience trying to use HTTPWebRequest and WebClient objects. And I found a great tool that should make it easy - Fiddler Request To Code.
But I had trouble with cookies SSL/TSL certs. Frankly I don't understand them and have not found anything on the web that really teaches how they work. I have found some example code but I ind it confusing. (For instance, most of the code creates something called a credentials cache, and then uses the properties to form the web calls - but the strings I end up collecting are wrong).
So in a previous project I was able to avoid all of that by using a webbrowser control.
With that said, I want to do my next version as a web service, so I'll need to figure this out - but for the moment I want to get version 1 off my to do list.
I am appreciative of hints and ideas....

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900