Reputation: 6653
What I have is simple, a form with in that form a webbrowser-controll.
That webbrowser goes to a website. And that all works.
But what I wan't to do when he's on the page is to let him run some javascript code (or jQuery if that's possible) to get all the text thats between the <b></b>
tags on the website.
How should I do that?
EDIT: Thanks to Birk, the code now looks like this:
HtmlElementCollection bigFontTags = webBrowser1.Document.GetElementById("Frame_A").Document.GetElementsByTagName("b");
string[] textPieces = new string[bigFontTags.Count];
for (int i = 0; i < bigFontTags.Count; i++) {
textPieces[i] = bigFontTags[i].InnerText;
}
//process text
string bigText = String.Join(" ", textPieces);
MessageBox.Show(bigText);
But that doesn't work. That is because the element that i need to get, is deep inside frame's and frameset's.... This is the layout (simple):
<frameset rows="0,1000" border="0" frameborder="0">
<frame src="/mail/Login?domain=telfort.nl&style=default&plain=0" name="SContentFrame" id="SContentFrame" scrolling="Auto" noresize="">
<frameset rows="88,*" framespacing="0" frameborder="0" border="0" bordercolor="#FFFFFF">
<frameset cols="182,*" framespacing="0" frameborder="0" border="0" bordercolor="#FFFFFF" onunload="ExitLogout()" onload="LoadFrames('/mail/Navigation?sid=1FBE4F29181F18D9358ABC082C7DEE1B6C67481B&userid=marcmeesters%40telfort.nl&seq=+Q&auth=+A&style=default','/mail/MessageList?sid=1FBE4F29181F18D9358ABC082C7DEE1B6C67481B&userid=marcmeesters%40telfort.nl&seq=+Q&auth=+A&srcfolder=INBOX&chk=1&style=default')">
<frame src="/supp/blank.htm" name="Frame_A" id="Frame_A" frameborder="0" scrolling="Yes" marginwidth="0" marginheight="0" bordercolor="#FFFFFF" border="0">
<form name="phoenix" method="post" action="" onsubmit="return clickedButton">
And then are here some tables and the content that i want to get.....
So how can i get that content from there??? (in a nice way???)
EDIT2: I will post the generated source of the website: (NOTE! i've deleted everything that wassn't relevant... So only the way from the begin to the content)
<html><head>
<title>Telfort - Webmail</title>
<link rel="SHORTCUT ICON" href="http://www.telfort.nl/images/template/favicon.ico">
<link rel="stylesheet" href="/style/telfort/stylesheet.css" type="text/css" media="screen">
<!-- main.tpl 20001107 -->
<script type="text/javascript" src="/supp/phsec.js"></script>
<script type="text/javascript" src="/supp/client_sniffer.js"></script>
<script type="text/javascript" src="chrome-extension://bfbmjmiodbnnpllbbbfblcplfjjepjdn/js/injected.js"></script></head>
<frameset rows="0,1000" border="0" frameborder="0">
<frame src="/mail/Login?domain=telfort.nl&style=default&plain=0" name="SContentFrame" id="SContentFrame" scrolling="Auto" noresize="">
<html><head>
<title>Telfort - Webmail</title>
<link rel="SHORTCUT ICON" href="http://www.telfort.nl/images/template/favicon.ico">
<link rel="stylesheet" href="/style/telfort/stylesheet.css" type="text/css" media="screen">
<!-- main.tpl 20001107 -->
<script type="text/javascript" src="/supp/phsec.js"></script>
<script type="text/javascript" src="/supp/client_sniffer.js"></script>
</head>
<frameset rows="0,1000" border="0" frameborder="0">
<frame src="/mail/Login?domain=telfort.nl&style=default&plain=0" name="SContentFrame" id="SContentFrame" scrolling="Auto" noresize="">
<html><head>
<title>Telfort - Webmail</title>
<link rel="stylesheet" href="/style/telfort/stylesheet.css" type="text/css" media="screen">
<!-- main_frame.tpl 20060510 -->
<script src="/supp/phif.js" type="text/javascript"></script>
<script type="text/javascript">
<!--
function LoadFrames(t1,t2) {
NavWin(frames["Frame_NAV"],t1);
NavWin(frames["Frame_A"],t2);
}
function ExitLogout() {
window.open(NavURL("/mail/Logout?sid=5D3995C305E2F91F575EDF1E2F11F5D75A7DF6D8&userid=marcmeesters%40telfort.nl&seq=+Q&auth=+A&style=default&popup=1"),"Logout","resizable=yes,scrollbars=yes,status=0,width=10,height=10");
}
//-->
</script>
</head>
<frameset rows="88,*" framespacing="0" frameborder="0" border="0" bordercolor="#FFFFFF">
<frameset cols="182,*" framespacing="0" frameborder="0" border="0" bordercolor="#FFFFFF" onunload="ExitLogout()" onload="LoadFrames('/mail/Navigation?sid=5D3995C305E2F91F575EDF1E2F11F5D75A7DF6D8&userid=marcmeesters%40telfort.nl&seq=+Q&auth=+A&style=default','/mail/MessageList?sid=5D3995C305E2F91F575EDF1E2F11F5D75A7DF6D8&userid=marcmeesters%40telfort.nl&seq=+Q&auth=+A&srcfolder=INBOX&chk=1&style=default')">
<frame src="/supp/blank.htm" name="Frame_A" id="Frame_A" frameborder="0" scrolling="Yes" marginwidth="0" marginheight="0" bordercolor="#FFFFFF" border="0">
<html><head>
<title>Berichtenlijst</title>
<!-- folders_msglist.tpl 20001106 -->
<link rel="stylesheet" href="/style/telfort/stylesheet.css" type="text/css" media="screen">
</head>
<body marginwidth="0" marginheight="0">
<form name="phoenix" method="post" action="" onsubmit="return clickedButton">
AND HERE THE CONTENT!!!!
</form>
</frameset>
</frameset>
</frameset>
</frameset>
</html>
EDIT 3 : Thanks to Birk, the problem is solved, and this is the answer:
HtmlWindow SContentFrame = webBrowser1.Document.Window.Frames[1];
HtmlWindow Frame_A = SContentFrame.Document.Window.Frames[2];
HtmlElementCollection bigFontTags = Frame_A.Document.GetElementsByTagName("b");
string[] textPieces = new string[bigFontTags.Count];
for (int i = 0; i < bigFontTags.Count; i++) {
textPieces[i] = bigFontTags[i].InnerText;
}
string bigText = String.Join(" ", textPieces);
MessageBox.Show(bigText);
Upvotes: 0
Views: 1891
Reputation: 1714
Create a DocumentCompleted-event and process all tags using ((WebBrowser)sender).Document.GetElementsByTagName("b");
.
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
HtmlElementCollection bigFontTags = ((WebBrowser)sender).Document.GetElementsByTagName("b");
string[] textPieces=new string[bigFontTags.Count];
for (int i = 0; i < bigFontTags.Count; i++)
{
textPieces[i] = bigFontTags[i].InnerText;
}
//process text
string bigText = String.Join(" ", textPieces);
MessageBox.Show(bigText);
}
That's not JavaScript, but the result is exactly the same.
EDIT #2: For all those nested frames you could use:
HtmlElementCollection bigFontTags = webBrowser1.Document.GetElementById("SContentFrame").Document.GetElementById("SContentFrame").Document.GetElementById("Frame_A").Document.GetElementsByTagName("b");
That should work, if I understand your frameset structure correctly.
Upvotes: 2