C#的webBrowser中,搜索元素中的某些文字

freewind1949 2018-04-17 08:52:27
想用webBrowser控件,导航到以下内容的页面,搜索"济芸斋" 三个字,返回 “米面油专用资格券18041601”,但搜索这块,总处理不好。


<div class="voucher-item voucher-gray-item" style="width:280px;height:122px;">
<div class="v-type" style="padding: 10px 0px 0px 15px;">
<div class="v-price">
<div class="clearfix" style="width:185px;height:45px;overflow:hidden;">
<em></em>
<strong class="num" style="margin:0;font-size: 12px;line-height: 16px;">【济芸斋】名润大地-200盘锦蟹田珍珠米(5KG/袋)赠送奖券3张</strong>
</div>
<div class="txt" style="line-height:1;position:relative;top:6px;float:none;">
<div class="limit"><span class="ftx-06">米面油专用资格券18041601</span></div>
</div>
</div>
<div class="v-range">
<div class="range-item">
<p title="最多可领取[1]张</p>
</div>
<div class="range-item">
2018.04.16-2018.04.17
</div>
</div>
</div>
<div class="v-opbtns" style="height:122px;padding-top:13px;">
<b class="semi-circle"></b>
<span>已领完</span>
</div>
<div class="v-state">
<div class="btn-state btn-getend"></div>
</div>
</div>


             int searchCount = 0;
label1.Text = textBoxKey.Text;
while (webBrowser1.ReadyState < WebBrowserReadyState.Complete)
Application.DoEvents(); //若没加载完则继续加载

HtmlElementCollection div = webBrowser1.Document.GetElementsByTagName("div");
for (int i = 0; i < div.Count; i++)
{
//div[contains(.,'text2')]/span[@class='value']
if (div[i].GetAttribute("span").Contains("济芸斋"))
// if(div[i].GetElementsByTagName(label1.Text))
{
HtmlElement divnk = div[i + 2].FirstChild;
MessageBox.Show(divnk.InnerHtml);
searchCount++;
}


}
if(searchCount==0)
MessageBox.Show("查不到要找的内容");



...全文
633 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
freewind1949 2018-04-18
  • 打赏
  • 举报
回复
早上研究一小时,搞定了。
  private void button1_Click(object sender, EventArgs e)
        {
            int searchCount = 0;
            label1.Text = textBoxKey.Text;
         //   while (webBrowser1.ReadyState < WebBrowserReadyState.Complete)
          //      Application.DoEvents();          //若没加载完则继续加载  

            HtmlElementCollection div = webBrowser1.Document.GetElementsByTagName("span");
            for (int i = 0; i < div.Count; i++)
            {
               
                if (div[i].InnerHtml != null && div[i].InnerHtml.Contains(label1.Text))
                {
                    label2.Text = div[i].InnerHtml;
                    if (div[i + 1].InnerHtml != null)
                    {                        
                        MessageBox.Show(div[i].InnerHtml +" "+ div[i + 1].InnerHtml);
                        searchCount++;
                    }
                }               
                
            }

            if (searchCount == 0)
                label2.Text = "查不到要找的内容";
            else
                label2.Text = (label2.Text+" 共找到 "+searchCount+" 处");

        }

28,390

社区成员

发帖
与我相关
我的任务
社区描述
ASP即Active Server Pages,是Microsoft公司开发的服务器端脚本环境。
社区管理员
  • ASP
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧