编程选择从下拉一个项目下来.NET WebBrowser控件 [英] Programmatically select an item from a drop down in .NET Webbrowser Control

查看:237
本文介绍了编程选择从下拉一个项目下来.NET WebBrowser控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是一个HTML脚本,我从网站上抓起。我想选择该项目以编程方式使用.NET

Below is a html script, I grabbed from a website. I wanna select the item programmatically using .NET

<div id="MySite.condition_s-wrp" class="c-wrp-slctbx" style="z-index: 1;">
    <input id="MySite.condition_s-input" type="text" autocomplete="off" readonly="readonly" tabindex="0" class=" c-slctbx-medium" style="width: 268px;">
    <ul class="c-ul-slctbx max_height_300" style="width: 285px; display: none; top: 21px;">
        <li id="MySite.condition_s-option-" class="c-li-slctbx">Please choose</li>
        <li id="MySite.condition_s-option-First" class="c-li-slctbx">First</li>
        <li id="MySite.condition_s-option-Second" class="c-li-slctbx">Second</li>
    </ul>
    <select id="MySite.condition_s" name="attributeMap[MySite.condition_s]" class=" c-slctbx-medium" style="display: none;">
        <option value="">Please choose</option>
        <option value="First">First</option>
        <option value="Second">Second</option>
        </select>
</div>

请注意以下code是不工作的。

Please note the following code is not working at all.

webBrowser1.Document.GetElementById("MySite.condition_s").SetAttribute("value", "First");

任何快速帮助将是非常美联社preciated。

Any quick help will be highly appreciated.

推荐答案

我终于弄明白跟我的一个朋友。这个小功能很容易做休息。

Finally I figure it out with one of my friends. This little function will do the rest very easily.

由于法鲁克莫明和他的时间。

Thanks to Farrukh Momin and his time.

    public void SetComboItem(string id, string value) {
        HtmlElement ee = this.webBrowser1.Document.GetElementById(id);
        foreach (HtmlElement item in ee.Children) {
            if (item.OuterHtml.ToLower().IndexOf(value.ToLower()) >= 0) {
                item.SetAttribute("selected", "selected");
                item.InvokeMember("onChange");
            }
            else {
                item.SetAttribute("selected", "");
            }
        }

        ee = this.webBrowser1.Document.GetElementById(id + "-input");
        ee.InnerText = value;
    }

调用功能

    this.SetComboItem("MySite.condition_s", "First");

这篇关于编程选择从下拉一个项目下来.NET WebBrowser控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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