+ -
当前位置:首页 → 问答吧 → WebBrowser 怎么获取下拉框的数量或值

WebBrowser 怎么获取下拉框的数量或值

时间:2011-12-14

来源:互联网

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

作者: SaberIII   发布时间: 2011-12-14

用 htmldocument 解析吧,get by class or ID

作者: startstartsvip   发布时间: 2011-12-14


我记得我以前也有这个问题,但好像不能直接获取到,只能通过html来分析了

作者: bunliney   发布时间: 2011-12-14

解析网页,抓取下拉框的值

作者: ssp2009   发布时间: 2011-12-14

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

            }
        }



测试HTML
HTML code

<html>
<head>
<title>test</title>
</head>
<body>
请选择:
<select name="selectAge" id="selectAge">  
        <option value="1">18-21</option>  
        <option value="2">22-25</option>  
        <option value="3">26-29</option>  
        <option value="4">30-35</option>  
        <option value="5">Over35</option>  
</select>  

</body>


作者: sdfkfkd   发布时间: 2011-12-14

引用 4 楼 sdfkfkd 的回复:

C# code


private void Test()
{

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

……


非常之好,可以问一下哪里有这方面知识可以看吗 是网页或者书?

作者: SaberIII   发布时间: 2011-12-17