获取webbrowser当前网页中的某文本框的值

wang520d 2008-08-20 11:53:42
问题一:不知道webbrowser是否有加载完成一个网页的事件,因为这段代码放在 form_load里面得不到任何网页代码
HtmlDocument hd = getCurrentBrowser().Document;
//webbrowser1.document.body.innertext
//hd.GetElementById("")
MessageBox.Show(hd.Body.InnerText.ToString() + hd.Body.InnerHtml.ToString());

我怀疑是网页还没有加载完成,或者有延时?
问题二:如题
...全文
742 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
lovemumu1314 2010-07-08
  • 打赏
  • 举报
回复
那如何在webbrowser中写文本框事件呢?前提是文本框是获取元素得到的而不是直接在webbrowser中添加的
宝_爸 2008-08-20
  • 打赏
  • 举报
回复
不是,是回答你这个问题
----------------------
请问下 怎么结合ProgressChanged判断。。
----------------------


文本框值改变后的处理吗?这个问题还没有找到答案。
可以在页面里写javascript,但是不知道怎么通知外面的application
wang520d 2008-08-20
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 findcaiyzh 的回复:]
CurrentProgress == e.MaximumProgress 就说明载入完成了。
private void WebBrowser1_ProgressChanged(Object sender, WebBrowserProgressChangedEventArgs e) {

System.Text.StringBuilder messageBoxCS = new System.Text.StringBuilder();
messageBoxCS.AppendFormat("{0} = {1}", "CurrentProgress", e.CurrentProgress );
messageBoxCS.AppendLine();
messageBoxCS.AppendFormat("{0} = {1}", "MaximumProgres…
[/Quote]
这个是解决 文本框值改变后的处理吗? 刚刚接触这个控件 太多不懂。。。见谅!
宝_爸 2008-08-20
  • 打赏
  • 举报
回复
CurrentProgress == e.MaximumProgress 就说明载入完成了。
private void WebBrowser1_ProgressChanged(Object sender, WebBrowserProgressChangedEventArgs e) {

System.Text.StringBuilder messageBoxCS = new System.Text.StringBuilder();
messageBoxCS.AppendFormat("{0} = {1}", "CurrentProgress", e.CurrentProgress );
messageBoxCS.AppendLine();
messageBoxCS.AppendFormat("{0} = {1}", "MaximumProgress", e.MaximumProgress );
messageBoxCS.AppendLine();
MessageBox.Show(messageBoxCS.ToString(), "ProgressChanged Event" );
}
宝_爸 2008-08-20
  • 打赏
  • 举报
回复
你自己做个delegate,在修改完值的时候,自己fire它。
wang520d 2008-08-20
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 kwork 的回复:]
问题1:DocumentCompleted事件。不一定完全准确,结合ProgressChanged判断就更准确了。
问题2:第二个问题,没太明白,你是指的在WebBrowser里面或者号码文本框的值还是在你的框架网页里面。
如果是在WebBrowser里面,那么用
HtmlDocument htmlDoc = wb.Document;
HtmlElementCollection myspan = htmlDoc.GetElementById("myH2");
myspan[0].value 就是你要的号码框的值

如果你是要在你的框架网页里面取,那么要看…
[/Quote]

请问下 怎么结合ProgressChanged判断。。
wang520d 2008-08-20
  • 打赏
  • 举报
回复
第二个问题 上面两位朋友的都可以 我自己也找了种方面

IHTMLDocument2 doc = (IHTMLDocument2)getCurrentBrowser().Document.DomDocument;

mshtml.HTMLInputTextElement text1;

text1 = (HTMLInputTextElement)doc.all.item("input_id", 0);
MessageBox.Show(text1.value);
((mshtml.HTMLInputElement)(text1)).value = "warran";

可问题又来了

能不能当这个文本框的值改变时 让webbrowser捕捉到?????
宝_爸 2008-08-20
  • 打赏
  • 举报
回复
第二个问题的答案:
HtmlDocument doc = webBrowser1.Document;
HtmlElement element = doc.GetElementById("txtCustEmail"); //Id,就是你的myH2

string value = element.GetAttribute("value"); //取得Id的value attribute
kwork 2008-08-20
  • 打赏
  • 举报
回复
问题1:DocumentCompleted事件。不一定完全准确,结合ProgressChanged判断就更准确了。
问题2:第二个问题,没太明白,你是指的在WebBrowser里面或者号码文本框的值还是在你的框架网页里面。
如果是在WebBrowser里面,那么用
HtmlDocument htmlDoc = wb.Document;
HtmlElementCollection myspan = htmlDoc.GetElementById("myH2");
myspan[0].value 就是你要的号码框的值

如果你是要在你的框架网页里面取,那么要看你外面那个页面,和里面的是什么关系,同理一样,可以啊javascript用getid取得对应的元素。
wang520d 2008-08-20
  • 打赏
  • 举报
回复
谢谢上面两位 那第二个问题 哪个高手能帮忙啊。。。
我的思路 可是运行获取不到
//mshtml.IHTMLElementCollection collection = doc.all as mshtml.IHTMLDocument2; 
//collection
//mshtml.IHTMLDocument2 doc = (mshtml.IHTMLDocument2)getCurrentBrowser().Document;
//mshtml.IHTMLInputElement input = (mshtml.IHTMLInputElement)doc.all.item("input_id", 0);//这句报错了input.value;
wdgphc 2008-08-20
  • 打赏
  • 举报
回复
webBrowser有加载完页面的事件啊.


private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
}
宝_爸 2008-08-20
  • 打赏
  • 举报
回复
问题1:
WebBrowser..::.DocumentCompleted Event
Occurs when the WebBrowser control finishes loading a document.

wang520d 2008-08-20
  • 打赏
  • 举报
回复
注意下我的网页是框架页面 网页源码如下:
希望获得该网页中 号码文本框的值

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Main1.aspx.cs" Inherits="Main1" %>

<%@ Register Assembly="EeekSoft.Web.PopupWin" Namespace="EeekSoft.Web" TagPrefix="cc1" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">

<title>汉翔软通电视购物管理系统</title>

</head>
<body>


<form id="agentform" name="agentform" runat="server">
<table style="width: 100%; height: 100%; table-layout: fixed" border="0" cellpadding="0"
cellspacing="0">
<tr>
<td height="31">
<table cellpadding="0" cellspacing="0" border="0" width="100%">
<tr>
<td class="TopLeft">
<span class="TopInfo">
<asp:Literal ID="txtStf" runat="server"></asp:Literal></span></td>
<td class="TopBg">
<table cellpadding="0" cellspacing="0" border="0">
<tr>
<td class="TopAle" nowrap>
</td>
<td class="TopTxt" nowrap>
号码:<asp:TextBox ID="myH2" runat="server" Style="background-color: GreenYellow; width: 110px;"></asp:TextBox>
</td>
<td class="TopAle" nowrap>
</td>

<td class="TopTxt" nowrap>
</td>
<td class="TopAle" nowrap>
</td>
<td class="TopTxt" nowrap>

</td>
<td class="TopAle" nowrap>
</td>
<td class="TopTxt" nowrap>

</td>
</tr>
</table>
</td>
<td class="TopRightImg">
</td>
<td class="TopRightBg">
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td>
<table cellpadding="0" cellspacing="0" border="0" width="100%" height="100%">
<tr>
<td style="width: 175px; vertical-align: top; background: #E7F5FD;" id="frmleft">
<iframe src="Left2.aspx" style="width: 100%; height: 100%" frameborder="0"></iframe>
</td>
<td style="width: 6px; background: url(Images/switchbg.gif); vertical-align: middle;
white-space: nowrap;" id="splitBar">
<img src="Images/splitBar.gif" id="switchPoint" onclick="switchSysBar()" style="cursor: hand"
title="关闭/打开左边导航栏" alt="关闭/打开左边导航栏" />
</td>
<td>
<table cellpadding="0" cellspacing="0" border="0" height="100%" width="100%">
<tr>
<td>
<iframe name="MainFrame" id="MainFrame" src="Desktop.aspx" style="width: 100%; height: 100%;"
frameborder="0"></iframe>
</td>
</tr>
<tr>
<td height="20px">
<iframe src="Bottom.aspx" scrolling="no" style="width: 100%; height: 20px" frameborder="0">
</iframe>
</td>
</tr>
</table>
</td>
</tr>
</table>
</td>
</tr>
</table>
</form>

</body>
</html>

wang520d 2008-08-20
  • 打赏
  • 举报
回复
[Quote=引用 17 楼 findcaiyzh 的回复:]
我举得应该在页面载入完成后加入比较合适。

也就是DocumentCompleted事件中。
[/Quote]

呵呵 谢谢 搞定了。。。
宝_爸 2008-08-20
  • 打赏
  • 举报
回复
我举得应该在页面载入完成后加入比较合适。

也就是DocumentCompleted事件中。
wang520d 2008-08-20
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 findcaiyzh 的回复:]
HtmlElement有个方法叫AttachEventHandler,可以在C#中实现html的事件。


C# code public void HtmlTextChanged(Object sender, EventArgs e)
{
Console.WriteLine("Enter TextChanged");
}

private void button2_Click(object sender, EventArgs e)
{
HtmlDocument doc = webBrowser1.Document;
HtmlElement element = doc.GetElem…
[/Quote]

==========
element.AttachEventHandler("onpropertychange", new EventHandler(HtmlTextChanged));
这个事件在什么时候加合适呢?
宝_爸 2008-08-20
  • 打赏
  • 举报
回复
HtmlElement有个方法叫AttachEventHandler,可以在C#中实现html的事件。

 public void HtmlTextChanged(Object sender, EventArgs e)
{
Console.WriteLine("Enter TextChanged");
}

private void button2_Click(object sender, EventArgs e)
{
HtmlDocument doc = webBrowser1.Document;
HtmlElement element = doc.GetElementById("txtCustEmail");

element.AttachEventHandler("onpropertychange", new EventHandler(HtmlTextChanged));

string value = element.GetAttribute("value");
element.SetAttribute("value","new email");


}
wang520d 2008-08-20
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 findcaiyzh 的回复:]
不是,是回答你这个问题
----------------------
请问下 怎么结合ProgressChanged判断。。
----------------------


文本框值改变后的处理吗?这个问题还没有找到答案。
可以在页面里写javascript,但是不知道怎么通知外面的application
[/Quote]

我其实有个想法:在timer事件中 执行网页的一段脚本 这段脚本就是记录网页文本框onchage事件的 只是这样感觉效率就低了。。。唉。。。
wang520d 2008-08-20
  • 打赏
  • 举报
回复
最后一个问题我再描述一遍:就是当网页中的文本框的值发生改变能否通知webbrowser并让其获取改变后的值????

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

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