简体   繁体   English

在webbrowser控件中执行javascript

[英]Execute javascript in webbrowser control

I am trying to resolve the links in a google search results list by executing a javascript on the page that would normally resolve when you click the link. 我试图通过执行页面上的javascript来解析google搜索结果列表中的链接。 Basically, I am trying to resolve all of them when the page loads. 基本上,我试图在页面加载时解决所有问题。

Here is the html generated by google of the first two search results: 这是由Google生成的前两个搜索结果的html:

<H2 class=hd>Search Results</H2>
<DIV id=ires>
<OL id=rso eid="3gHFUYToG6LD0gGJ2IDAAQ">
<LI class=g>
<DIV class=rc data-hveid="43"><SPAN style="FLOAT: left"></SPAN>
<H3 class=r><A onmousedown="return rwt(this,'','','','1','AFQjCNH7JvhrnBRwX-7DKsGJZoasX9zqFA','','0CCwQFjAA','','',event)" href="http://prezi.com/wnx5jat2oene/ipad/">IPad by <EM>Frunk La Je</EM> on Prezi</A></H3>
<DIV class=s>
<DIV>
<DIV style="WHITE-SPACE: nowrap" class="f kv"><CITE>prezi.com/wnx5jat2oene/ipad/</CITE>‎
<DIV class="action-menu ab_ctl"><A aria-haspopup=true aria-expanded=false id=am-b0 class="clickable-dropdown-arrow ab_button" role=button href="#" jsaction="ab.tdd; keydown:ab.hbke; keypress:ab.mskpe" data-ved="0CC0Q7B0wAA" aria-label="Result details"><SPAN class=mn-dwn-arw></SPAN></A>
<DIV class="action-menu-panel ab_dropdown" role=menu tabIndex=-1 jsaction="keydown:ab.hdke; mouseover:ab.hdhne; mouseout:ab.hdhue" data-ved="0CC4QqR8wAA">
<UL>
<LI class="action-menu-item ab_dropdownitem" role=menuitem><A class=fl onmousedown="return rwt(this,'','','','1','AFQjCNHsvjmPpYAOeqVOHIIs_PWPuD5JaQ','','0CC8QIDAA','','',event)" href="http://webcache.googleusercontent.com/search?q=cache:bKCj-2ogWdIJ:prezi.com/wnx5jat2oene/ipad/+frunk+la+je&amp;cd=1&amp;hl=en&amp;ct=clnk&amp;gl=us">Cached</A></LI></UL></DIV></DIV></DIV>
<DIV class="f slp"></DIV><SPAN class=st>IPad. No description. by <EM>Frunk La Je</EM> on 7 June 2012 4 Tweet <B>...</B> thanks&lt;/a&gt;&lt;/p&gt;. Make a copy Share Embed Like. by <EM>Frunk La Je</EM> on 7 June 2012 4.</SPAN></DIV></DIV></DIV></LI>
<LI class=g>
<DIV class=rc data-hveid="48"><SPAN style="FLOAT: left"></SPAN>
<H3 class=r><A onmousedown="return rwt(this,'','','','2','AFQjCNE7-nvy-ymTrBI0x4zleJOUXlY6nA','','0CDEQFjAB','','',event)" href="http://prezi.com/user/q53evqg1dnrn/"><EM>Frunk La Je</EM> on Prezi</A></H3>
<DIV class=s>

The function that resolves the first link: 解析第一个链接的函数:

onmousedown="return rwt(this,'','','','1','AFQjCNH7JvhrnBRwX-7DKsGJZoasX9zqFA','','0CCwQFjAA','','',event)"

I tried this: 我尝试了这个:

    foreach (HtmlElement h in web.Document.All)
    {
        h.RaiseEvent("onmousedown");
    }

I tried replacing "onmousedown" with "MouseDown". 我尝试用“ MouseDown”替换“ onmousedown”。

I tried this too: 我也尝试过这个:

web.Document.InvokeScript("rwt(this,'','','','1','AFQjCNH7JvhrnBRwX-7DKsGJZoasX9zqFA','','0CCwQFjAA','','',event)");

Any ideas how I can get this function to execute? 有什么想法可以让我执行此功能吗? I've also looked at the getElementByID function, but I don't know what the ID is for this. 我还查看了getElementByID函数,但我不知道该ID是什么。

Try to simulate a click programmatically, let's say in one of your anchor tag <a> . 尝试以编程方式模拟点击 ,比方说您的anchor tag <a> Now using your own code we will simulate a click on your first anchor tag which is subscript 0 (as shown below), like this: 现在,使用您自己的代码,我们将模拟单击first anchor tag subscript 0 first anchor tag (如下所示),如下所示:

 HtmlElement simulateClickAnchor = webBrowser1.Document.GetElementsByTagName("a")[0];
 simulateClickAnchor.InvokeMember("click");
HtmlDocument doc = browser.Document;
HtmlElement head = doc.GetElementsByTagName("head")[0];
HtmlElement s = doc.CreateElement("script");
s.SetAttribute("text","function sayhello() { alert('hello'); }");
head.AppendChild(s);
browser.Document.InvokeScript("sayHello");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM