如何使用C#webbrowser控件从下拉列表中进行选择并调用提交脚本 [英] How to use C# webbrowser control to select from dropdowns and invoke submit script

查看:84
本文介绍了如何使用C#webbrowser控件从下拉列表中进行选择并调用提交脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C#winform项目中,我使用webbrowser对象登录网站,导航到特定列表,然后使用该列表。登录后,我需要从表单中的一些下拉选择选项中进行选择,然后提交表单(没有提交元素 - 通过单击以onclick事件为基础的图像来提交表单,全部如下所示。



这是目标网站代码的缩写版本(我已经将它缩短了一点 - 我想我已经把所有相关内容都删了。我也剪了将所有样式集中在HTML和javascript上。这是代码:



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





我不确定我是否正确地做了这两个,而对于第三个,没有ID,所以我不知道如何选择它。



最后,我想点击锚定的图像或调用单元格中的onclick脚本:





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.

推荐答案

您无需选择控件。您只需要知道正确的url / querystring格式/内容,并使用 WebClient 对象提交。
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.


这篇关于如何使用C#webbrowser控件从下拉列表中进行选择并调用提交脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆