点击WebBrowser控件内的HTML链接 [英] Click an HTML link inside a WebBrowser Control
问题描述
C#的Visual Studio 2010
C# Visual Studio 2010
我加载一个复杂的HTML页面到WebBrowser控件。但是,我没有修改网页的能力。我想从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).
这是在href链接的内容:
This is the content of the a href link:
<a
ID =u_lp_id_58547
HREF =JavaScript的:无效(0)
类=SGLeftPanelText的onclick =setStoreParams('案件; 212',212); window.leftpanel.onClick('cases_ss_733');返回false;&GT;
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());
我曾尝试在每一帧中插入名称和标记名现场试用过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屋!