2

First of all, sorry for my bad English.

The question is very simple. I want to click on a link with C# (windows forms)

This is the code that I use:

int number_to_show = Convert.ToInt32(listView1_new_message.SelectedItems[0].SubItems[4].Text);
            int a_to_click = (number_to_show * 5) + 2;
            string titel_of_message = listView1_new_message.SelectedItems[0].SubItems[1].Text;
            HtmlWindow SContentFrame = webBrowser1.Document.Window.Frames[1];
            HtmlWindow Frame_A = SContentFrame.Document.Window.Frames[2];
            HtmlElementCollection links = Frame_A.Document.GetElementsByTagName("b");
            int link_counter = 0;
            foreach (HtmlElement link in links) {
                if (link_counter == a_to_click && link.InnerText.Equals(titel_of_message)) {
                    link.InvokeMember("Click");
                }
                link_counter++;
            }

And it wants to click (I see it happening in debugging, and when I do a messagebox with the clicking, the messagebox appears), but the webpage don't react.

This is the htmlcode that is in the tag on the webpage:

<b><a href="" onclick="OnPhoenixRead('/mail/MessageRead?sid=7613BA4BF987D55FD171D53FAA24A259C7FDE435&amp;userid=*******&amp;seq=+Q&amp;auth=+A&amp;srcfolder=INBOX&amp;uid=3291&amp;srch=0&amp;style=default'); return false">Doorgest: testing 2</a></b>

So somehow it doesn't click on the text... (or doesn't activate the onclick event handler)

How can I make the program work like it should be?

Thanks in advance :D TWCrap

EDIT 1 This is the piece of code of the table holding al the emails....

<table width="100%" border="0" cellspacing="0" cellpadding="0" align="center">
<tbody><tr valign="middle" bgcolor="#f0f0f0">
<td width="20" align="center"><input type="checkbox" name="CheckAll" onclick="ToggleCheckAll()"></td>
<td width="1"><img src="/graphics/telfort/null.gif" height="1" width="1"></td>
<td width="20" align="center"><font face="Verdana" size="2" color="#141F78"><b>!!</b></font></td>
<td width="1"><img src="/graphics/telfort/null.gif" height="1" width="1"></td>
<td width="20" align="center"><img src="/graphics/telfort/mail-icon.gif" width="20" height="20" border="0" alt=""></td>
<td width="1"><img src="/graphics/telfort/null.gif" height="1" width="1"></td>
<td width="20" align="center"><img src="/graphics/telfort/attachments.gif" width="20" height="21" border="0" alt=""></td>
<td width="1"><img src="/graphics/telfort/null.gif" height="1" width="1"></td>
<td class="mailtitlebar">&nbsp;<a href="" onclick="NavA('/mail/MessageSort?sid=3B886D11EEAAF546F83EFAA8506E56BC30949BC9&amp;userid=marcmeesters%40telfort.nl&amp;seq=+Q&amp;auth=+A&amp;srcfolder=INBOX&amp;sort=1&amp;style=default&amp;start=100&amp;end=1'); return false">Van&nbsp;</a>&nbsp;</td>
<td width="1"><img src="/graphics/telfort/null.gif" height="1" width="1"></td>
<td class="mailtitlebar"><a href="" onclick="NavA('/mail/MessageSort?sid=3B886D11EEAAF546F83EFAA8506E56BC30949BC9&amp;userid=marcmeesters%40telfort.nl&amp;seq=+Q&amp;auth=+A&amp;srcfolder=INBOX&amp;sort=5&amp;style=default&amp;start=100&amp;end=1'); return false">Onderwerp&nbsp;</a>&nbsp;</td>
<td width="1"><img src="/graphics/telfort/null.gif" height="1" width="1"></td>
<td class="mailtitlebar"><a href="" onclick="NavA('/mail/MessageSort?sid=3B886D11EEAAF546F83EFAA8506E56BC30949BC9&amp;userid=marcmeesters%40telfort.nl&amp;seq=+Q&amp;auth=+A&amp;srcfolder=INBOX&amp;sort=7&amp;style=default&amp;start=100&amp;end=1'); return false">Ontvangen&nbsp;</a>&nbsp;</td>
<td width="1"><img src="/graphics/telfort/null.gif" height="1" width="1"></td>
<td class="mailtitlebar"><a href="" onclick="NavA('/mail/MessageSort?sid=3B886D11EEAAF546F83EFAA8506E56BC30949BC9&amp;userid=marcmeesters%40telfort.nl&amp;seq=+Q&amp;auth=+A&amp;srcfolder=INBOX&amp;sort=9&amp;style=default&amp;start=100&amp;end=1'); return false">Grootte&nbsp;</a>&nbsp;</td>
</tr>
<tr><td colspan="15" bgcolor="#f0f0f0"><img src="/graphics/telfort/null.gif" height="1" width="1"></td></tr><!-- Message List Head --><!-- Message List --><tr valign="middle">
<td width="20" align="center" bgcolor="#f0f0f0"><input type="checkbox" name="msguid" value="3330"></td>
<td width="1" bgcolor="#f0f0f0"><img src="/graphics/telfort/null.gif" height="1" width="1"></td>
<td width="20" align="center">
<script>
var stat = '<IMG SRC="/graphics/priority_normal.gif" BORDER=0 width="10" height="18"><IMG SRC="/graphics/new.gif" BORDER=0 width="20" height="18"><IMG SRC="/graphics/attach_none.gif" BORDER=0 width="8" height="18">';
</script>
<script>
if (stat.indexOf('high.gif') > -1 )
{
document.write ('<font face="Tahoma" size="2" color="#141D7C"><b>!!</b></font>');
}
else if (stat.indexOf('low.gif') > -1 )
{
document.write ('<font face="Tahoma" size="2" color="#141D7C"><b>!</b></font>');
}
</script></td>
<td width="1" bgcolor="#f0f0f0"><img src="/graphics/telfort/null.gif" height="1" width="1"></td>
<td width="20" align="center" bgcolor="#ffffff"><script>
if (stat.indexOf('new.gif') > -1)
{
document.write ('<img src="/graphics/telfort/mail-icon.gif" width="20" height="20" border="0" alt="">');
}
else if (stat.indexOf('deleted.gif') > -1)
{
document.write ('<img src="/graphics/telfort/cancel.gif" width="20" height="20" border="0" alt="">');
}
else if (stat.indexOf('replied.gif') > -1)
{
document.write ('<img src="/graphics/telfort/reply.gif" width="20" height="20" border="0" alt="">');
}
else
{
document.write ('<img src="/graphics/telfort/mailopen-icon.gif" width="20" height="20" border="0" alt="">');
}
</script><img src="/graphics/telfort/mail-icon.gif" width="20" height="20" border="0" alt=""></td>
<td width="1" bgcolor="#ffffff"><img src="/graphics/telfort/null.gif" height="1" width="1"></td>
<td width="20" align="center" bgcolor="#ffffff"><script>
if (stat.indexOf('attach.gif') > -1)
{
document.write ('<img src="/graphics/telfort/attach-icon.gif" width="20" height="20" border="0" alt="">');
}
</script></td>
<td width="1" bgcolor="#ffffff"><img src="/graphics/telfort/null.gif" height="1" width="1"></td>
<td bgcolor="#ffffff" class="mailtxt">&nbsp;&nbsp;<b><a href="" onclick="OnPhoenixRead('/mail/MessageRead?sid=3B886D11EEAAF546F83EFAA8506E56BC30949BC9&amp;userid=marcmeesters%40telfort.nl&amp;seq=+Q&amp;auth=+A&amp;srcfolder=INBOX&amp;uid=3330&amp;srch=0&amp;style=default'); return false">Tweakers.net Nieuwsbrief</a></b></td>
<td width="1" bgcolor="#ffffff"><img src="/graphics/telfort/null.gif" height="1" width="1"></td>
<td bgcolor="#ffffff" class="mailtxt">&nbsp;&nbsp;<b><a href="" onclick="OnPhoenixRead('/mail/MessageRead?sid=3B886D11EEAAF546F83EFAA8506E56BC30949BC9&amp;userid=marcmeesters%40telfort.nl&amp;seq=+Q&amp;auth=+A&amp;srcfolder=INBOX&amp;uid=3330&amp;srch=0&amp;style=default'); return false">Samsung toont info eerste Windows Phone ...</a></b></td>
<td width="1" bgcolor="#ffffff"><img src="/graphics/telfort/null.gif" height="1" width="1"></td>
<td bgcolor="#ffffff" class="mailtxt">&nbsp;&nbsp;<b>jul 30</b></td>
<td width="1" bgcolor="#ffffff"><img src="/graphics/telfort/null.gif" height="1" width="1"></td>
<td bgcolor="#ffffff" class="mailtxt">&nbsp;&nbsp;<b>42.9 KB</b></td>
</tr>
<tr><td colspan="15" bgcolor="#bbbdbf"><img src="/graphics/telfort/null.gif" height="1" width="1"></td></tr>
<!-- Message List end--><tr valign="middle">
<td width="20" align="center" bgcolor="#f0f0f0"><input type="checkbox" name="msguid" value="3329"></td>
<td width="1" bgcolor="#f0f0f0"><img src="/graphics/telfort/null.gif" height="1" width="1"></td>
<td width="20" align="center">
<script>
var stat = '<IMG SRC="/graphics/priority_normal.gif" BORDER=0 width="10" height="18"><IMG SRC="/graphics/new.gif" BORDER=0 width="20" height="18"><IMG SRC="/graphics/attach_none.gif" BORDER=0 width="8" height="18">';
</script>
<script>
if (stat.indexOf('high.gif') > -1 )
{
document.write ('<font face="Tahoma" size="2" color="#141D7C"><b>!!</b></font>');
}
<!-- Message List end--></tbody></table>
Mathlight
  • 6,436
  • 17
  • 62
  • 107

5 Answers5

0

Maybe try changing

a href .. line into a button or some other structure that has onClick event. The way you are doing it, it seems just that href tries to go to the url that is not there ("")

Hope this is the issue.

trainoasis
  • 6,419
  • 12
  • 51
  • 82
0

Change the line that says

HtmlElementCollection links = Frame_A.Document.GetElementsByTagName("b");

to

HtmlElementCollection links = Frame_A.Document.GetElementsByTagName("a");

You're setting links equal to all elements with a bold tag. Because the link is within the bold tag, the innertext condition passes. You're then trying to click a bold tag, which obviously does nothing.

Tim S
  • 5,023
  • 1
  • 34
  • 34
  • I cant use the getAttribute on link ?!? this is the error then: Error 1 'System.Windows.Forms.HtmlElement' does not contain a definition for 'getAttribute' and no extension method 'getAttribute' accepting a first argument of type 'System.Windows.Forms.HtmlElement' could be found (are you missing a using directive or an assembly reference?) – Mathlight Jul 30 '12 at 15:13
  • Sorry, I think it should be GetAttribute (capital G), and IndexOf. Sorry, haven't coded in C# in a while. – Tim S Jul 30 '12 at 15:19
  • whell, i'm looking and trying, but it still doesn't work. if i go back to the old code ( if (link_counter == a_to_click && link.InnerText.Equals(titel_of_message)) { // link.InvokeMember("click"); MessageBox.Show(link.InnerText.ToString()); MessageBox.Show("geklikt"); } ) says it had cliked on it, and says the name. And that works great. But it still doesn't "click" and the onclick doesn't work either.... – Mathlight Jul 30 '12 at 15:42
  • Hmm, seems like it must be finding a different link. Can you share a link to the site so we can view the source? – Tim S Jul 30 '12 at 15:46
  • eeuhm, that just the problem. It's an webmail site, so you have to login, before you can view the source. And as you probably understand, am i not giving my username and password away... ;-) – Mathlight Jul 30 '12 at 15:48
  • You're answer is true, but there is only one problem. I've got readed mail, and unreaded mail. readed mail is just normal, but non readed mail is with bold tags. What i do is i get all the bold tags and make the form. I make an counter so i can find the message again later. This is becauses it can happen that the user, titel, and date, and size equals to together, so i don't get the wrong email. So if i select al the a tags instead of the bold tags, it will not select the right email.... – Mathlight Jul 30 '12 at 15:57
  • Ok, well this is really hard when you're not giving us any source to look at. Get all the "b" tags, loop through them. When it passes your innertext check, you need to extract the a tag out of it. So you'll have to declare a new variable of BrowserLink type, and see it to a sub element within the b link. OR, are the other links onclick property different? Can you check the onclick attribute for IndexOf("srcfolder=INBOX") > -1 or IndexOf("srcfolder=ReadMail") > -1 ? Is there something different there you can use to distinguish differences? – Tim S Jul 30 '12 at 16:03
0

If I understand you correctly, you want to click on a link from server side. I think that the best way to do this is to use JS.

For examples, lets assume that we have a link (simple tag) and a server-side button. When you click on a button, you want the link to be clicked automatically.

Solution: We need to return from server-side call a piece of Javascript code that would click on a link. This solutions uses jQuery.

Code-behind:

protected void btnTest_Click(object sender, EventArgs e)
{
    ClientScript.RegisterClientScriptBlock(typeof(Page), "ClickOnLink", "<script type='text/javascript'>$(document).ready(function () { var link = $('#linkToClick'); link.click(); }); </script>");
}

Page markup:

<asp:Button runat="server" ID="btnTest" OnClick="btnTest_Click" Text="Go!"/>
<a id="linkToClick" href="#" onclick="alert('Hi, Im clicked'); return false;">Click to alert</a>
berliner
  • 1,887
  • 3
  • 15
  • 23
0

You can always do it old-school: simulate input with the mouse.

First, get the possition of the link - use some point within its ClientRectangle (Middle works well) http://msdn.microsoft.com/de-de/library/system.windows.forms.htmlelement.clientrectangle.aspx

Next, move the mouse to that point Simulating mouse movement (C#)

and simulate a click How to simulate Mouse Click in C#?

Important: these links have to be on-screen, so make your HTML element large enough or insert some clever logic to scroll.. (or simply use ScrollIntoView http://msdn.microsoft.com/de-de/library/system.windows.forms.htmlelement.scrollintoview.aspx )

Community
  • 1
  • 1
Andreas Reiff
  • 7,961
  • 10
  • 50
  • 104
0

There are two things here:

  1. You want to click the link to go to a page.
  2. You want to trigger onClick event of a link.

1st one is a piece of cake. If you want the 2nd one, you are looking at a javascript engine for c# :D

And, yes a userscript can be a possible solution.

p.s. Please correct me if I am wrong.

EDIT: A JavaScript engine is a component of a browser and is usually bundled with it. It takes care of all JavaScript on the page. Now, cURL, or WebClient or wget do not have this engine. They just fetch you the plain html content. So, a better option to automate stuff that require JavaScript would be a userscript. A userscript if you haven't heard of it before, is a piece of javascript code that can be executed on pages you specify, there by allowing you to run code on a page as soon as you load it. Check out greasemonkey and userscripts. If you still want to know about using a JavaScript engine, you should wait for someone else to respond on that topic. I have not much idea about using one in standalone manner.

Prasanth
  • 5,230
  • 2
  • 29
  • 61
  • indeed, i want to click on the link, so the OnClick event handler will be triggert (because that opens an popup with the email)... – Mathlight Aug 03 '12 at 13:41
  • Hy, thanks for your reply. I've used userscripts before (for something else), but i like to do it with the program. So no (real) webbrowser open, just the program.... But thanks anyway ;-) – Mathlight Aug 06 '12 at 06:38