日期:2014-05-18  浏览次数:20811 次

WebBrowser 怎么获取下拉框的数量或值
如题,获取下拉框中有多少个值 或者获取第N个的值是多少

------解决方案--------------------
C# code


        private void Test()
        {

            //selectAge 为下拉框的ID
            HtmlElement element = this.webBrowser.Document.All["selectAge"];
            if (element != null) {

                //下拉框元素个数
                int count = element.Children.Count;
                
                //获取当前选择的元素索引
                object o = element.GetAttribute("selectedIndex");
                
                //获取当前选择的元素的value及text
                if (o != null && !string.IsNullOrEmpty(o.ToString()))
                {
                    int selectIndex = Convert.ToInt32(o);
                    o = element.Children[selectIndex].GetAttribute("value");
                    o = element.Children[selectIndex].GetAttribute("text");
                }

                //遍历每个元素
                foreach (HtmlElement item in element.Children)
                {
                    System.Diagnostics.Debug.WriteLine(string.Format("value:{0},text:{1}", item.GetAttribute("value"), item.GetAttribute("text")));
                }

            }
        }