最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - Programmatically select an item from a drop down in .NET Webbrowser Control - Stack Overflow

programmeradmin11浏览0评论

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>

Please note the following code is not working at all.

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

Any quick help will be highly appreciated.

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>

Please note the following code is not working at all.

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

Any quick help will be highly appreciated.

Share Improve this question asked Mar 7, 2013 at 7:35 Muhammad Sharjeel AhsanMuhammad Sharjeel Ahsan 6723 gold badges14 silver badges28 bronze badges 2
  • You could fetch the selects children and set the attribute selected on one of them. – Bart Friederichs Commented Mar 7, 2013 at 7:53
  • Explain "is not working at all". – Bart Friederichs Commented Mar 7, 2013 at 9:36
Add a comment  | 

6 Answers 6

Reset to default 6

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

Calling Function

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

Have you tried this:

webBrowser1.Document.GetElementById("MySite.condition_s").selectedIndex = 1

Try this.

   HtmlDocument document = webBrowser1.Document;
        HtmlElement siteCondition = document.GetElementById("MySite.condition_s");

        var option = siteCondition.Children.Cast<HtmlElement>().First(x => x.GetAttribute("value").Equals("First"));
        option.SetAttribute("selected", "selected");

here is your solution just go through example: http://www.vbforums.com/showthread.php?701093-Webbrowser-Control-Select-Dropdownlists-option

or

http://social.msdn.microsoft.com/Forums/en-US/Vsexpressvb/thread/b1273d78-d4af-49e0-9238-6f86e9952484/

I founded that if you just invoke click one by one, you should be able to find what you want by doing a for loop click inside it.

HtmlElement site = this.webBrowser2.Document.GetElementById("myId");
foreach (HtmlElement item in site.Children)
{ 
   if (item.InnerText.ToString() == "something")
   {
       item.InvokeMember("Click");
             break;
   }
       else
   {
       item.InvokeMember("Click");
   }
}

100% working code (tested on win7 - ie11)

taken from:

c# | WebBrowser control - programmatically select item on html select
http://mdb-blog.blogspot.com/2016/12/c-browser-control-programmatically.html

HtmlElementCollection col = webBrowser1.Document.GetElementsByTagName("select") 
foreach (HtmlElement heItem in col) 
{ 
  if (heItem.GetAttribute("className").Contains("exampleClassName") == true) 
  { 
    heItem.SetAttribute("selectedIndex", "3"); // select value at #3
    break; // incase of needed... 
  } 
} 
发布评论

评论列表(0)

  1. 暂无评论