点击网页链接

时间:2012-07-18 17:29:33

标签: c# winforms click webbrowser-control

首先,抱歉我的英语不好。

问题很简单。我想点击C#(windows窗体)的链接

这是我使用的代码:

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++;
            }

它想要点击(我看到它在调试中发生,当我点击消息框时,会出现消息框),但网页没有反应。

这是网页上 标签中的htmlcode:

<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>

所以它不会点击文本...(或者不激活oncl​​ick事件处理程序)

如何使程序按原样运行?

提前致谢:D TWCrap

编辑1 这是包含电子邮件的表格的代码......

<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>

5 个答案:

答案 0 :(得分:0)

也许尝试更改

一个href ..行到一个按钮或其他具有onClick事件的结构。 你这样做的方式,似乎只是href试图去那个不存在的网址(“”)

希望这是问题。

答案 1 :(得分:0)

更改显示

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

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

您使用粗体标记设置等于所有元素的链接。因为链接在粗体标记内,所以innertext条件通过。然后你试图点击一个粗体标签,这显然什么也没做。

答案 2 :(得分:0)

如果我理解正确,您想要点击服务器端的链接。 我认为最好的方法是使用JS。

例如,假设我们有一个链接(简单标记)和一个服务器端按钮。单击按钮时,您希望自动单击该链接。

解决方案: 我们需要从服务器端调用返回一个单击链接的Javascript代码。这个解决方案使用jQuery。

代码隐藏:

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

页面标记:

<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>

答案 3 :(得分:0)

你总是可以做旧学校:用鼠标模拟输入。

首先,获取链接的可能性 - 在其ClientRectangle中使用一些点(中间工作正常)http://msdn.microsoft.com/de-de/library/system.windows.forms.htmlelement.clientrectangle.aspx

接下来,将鼠标移动到该点Simulating mouse movement (C#)

并模拟点击How to simulate Mouse Click in C#?

重要提示:这些链接必须在屏幕上,因此要使您的HTML元素足够大或插入一些聪明的逻辑来滚动..(或只是使用ScrollIntoView http://msdn.microsoft.com/de-de/library/system.windows.forms.htmlelement.scrollintoview.aspx

答案 4 :(得分:0)

这里有两件事:

  1. 您想点击该链接转到某个页面。
  2. 您想要触发链接的onClick事件。
  3. 第一个是小菜一碟。如果你想要第二个,你正在寻找c#:D

    的javascript引擎

    并且,是的,用户脚本可能是一种可能的解决方案。

    P.S。如果我错了,请纠正我。

    编辑: JavaScript引擎是浏览器的一个组件,通常与它捆绑在一起。它负责页面上的所有JavaScript。现在,cURL,或WebClient或wget没有这个引擎。他们只是提取简单的HTML内容。因此,自动化需要JavaScript的东西的更好选择是用户脚本。如果您之前没有听说过,那么用户脚本就是一段可以在您指定的页面上执行的JavaScript代码,允许您在加载后立即在页面上运行代码。查看greasemonkeyuserscripts。如果您仍想了解使用JavaScript引擎,则应等待其他人回复该主题。我不太了解以独立方式使用它。

相关问题