单击 WebBrowser 控件内的 HTML 链接 [英] Click an HTML link inside a WebBrowser Control

查看:17
本文介绍了单击 WebBrowser 控件内的 HTML 链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C# Visual Studio 2010

C# Visual Studio 2010

我正在将复杂的 html 页面加载到网络浏览器控件中.但是,我没有能力修改网页.我想从 Windows 窗体中自动单击页面上的链接.但是,每次加载页面时,ID 似乎都是随机生成的(所以我相信引用 ID 是行不通的).

I am loading a complex html page into a webbrowser control. But, I don't have the ability to modify the webpage. I want to click a link on the page automatically from the windows form. But, the ID appears to be randomly generated each time the page is loaded (so I believe referencing the ID will not work).

这是a href链接的内容:

This is the content of the a href link:

<a 

id="u_lp_id_58547"href="javascript:void(0)"class="SGLeftPanelText" onclick="setStoreParams('cases;212', 212); window.leftpanel.onClick('cases_ss_733');return false; ">

My Assigned</a>

是否可以从 C# 中单击链接?

Is the anyway to click the link from C#?

谢谢!

更新:

我觉得这很接近,但它只是不起作用:

I feel like this is close but it is just not working:

HtmlElementCollection links = helpdeskWebBrowser.Document.Window.Frames["main_pending_events_frame"].Document.GetElementsByTagName("a");
MessageBox.Show(links.Count.ToString());

我尝试插入每个帧名称,并在 TagName 字段中尝试了a"和A",但没有任何运气.我找不到任何链接;消息框始终为 0.我错过了什么?

I have tried plugging in every single frame name and tried both "a" and "A" in the TagName field but just have not had any luck. I can just not find any links; the message box is always 0. What am I missing?

推荐答案

这样的事情应该可行:

HtmlElement link = webBrowser.Document.GetElementByID("u_lp_id_58547")
link.InvokeMember("Click")

由于 ID 是随机生成的,另一种选择可能是通过它们的 InnerText 来识别链接;沿着这些路线.

Since the IDs are generated randomly, another option may be to identify the links by their InnerText; along these lines.

HtmlElementCollection links = webBrowser.Document.GetElementsByTagName("A");

foreach (HtmlElement link in links)
{
    if (link.InnerText.Equals("My Assigned"))
        link.InvokeMember("Click");
}

更新:

您可以使用以下方法获取 IFrame 中的链接:

You can get the links within an IFrame using:

webBrowser.Document.Window.Frames["MyIFrame"].Document.GetElementsByTagName("A");

这篇关于单击 WebBrowser 控件内的 HTML 链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆