[英]How to click on link in awesomium webcontrol c#
I have a website that have two links in a div. 我有一个在div中有两个链接的网站。 I want to click on second link which is inside that div.
我想单击该div内的第二个链接。 Here is how website's HTML look like:
网站的HTML如下所示:
<div class="title-nav-single">
<strong></strong>
<a href="http://www.website.com/50665">Previous</a>
<a href="http://www.website.com/50665/3">Next</a>
</div>
Now I'm trying to click on the link that contains Next
. 现在,我试图单击包含
Next
的链接。 I don't want to get link href
value, I just want to click on the link. 我不想获取链接
href
值,我只想单击链接。 Here is what I have tried so far: 到目前为止,这是我尝试过的:
private void button1_Click(object sender, EventArgs e)
{
string xpath = GetJsSingleXpathString("//DIV[@ID=\"outbrain_widget_0\"]/preceding-sibling::DIV[3]//A[normalize-space()=\"Next\"]");
JsFireEvent(xpath, "click");
// webcontrol.ExecuteJavascriptWithResult("document.getElementsByClassName('title-nav-single').ElementAt(1).DomObject.click();");
}
public static string GetJsSingleXpathString(string xpath)
{
return String.Format("document.evaluate(\"{0}\", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue", xpath);
}
// executes javascript which fires specified event on element
// Example: JsFireEvent("document.getElementById('my_id')", "click");
public void JsFireEvent(string getElementQuery, string eventName)
{
webcontrol.ExecuteJavascript(@"function fireEvent(element,event) {
var evt = document.createEvent('HTMLEvents');
evt.initEvent(event, true, true ); // event type,bubbling,cancelable
element.dispatchEvent(evt);
}" + String.Format("fireEvent({0}, '{1}');", getElementQuery, eventName));
}
}
To detect if link was clicked you need to add target="_blank"
: 要检测是否单击了链接,您需要添加
target="_blank"
:
<a href="http://www.website.com/50665/3" target="_blank">Next</a>
and capture this event: 并捕获此事件:
webControl.ShowCreatedWebView += OnShowNewView;
internal static void OnShowNewView( object sender, ShowCreatedWebViewEventArgs e )
{
// Do sth with your link. it's in e.TargetURL
}
finally, you can open it with traditional way: 最后,您可以使用传统方式打开它:
System.Diagnostics.Process.Start(e.TargetURL.ToString());
..or do whatever else you want to do with it. ..或做您想做的其他事情。 More information you can find here
您可以在这里找到更多信息
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.