如何在awesomium webcontrol c#中点击link
How to click on link in awesomium webcontrol c#
我的网站在 div 中有两个 link。
我想点击 div 里面的第二个 link。
这是网站 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>
现在我试图点击包含 Next
的 link。我不想获得 link href
值,我只想单击 link。到目前为止,这是我尝试过的方法:
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));
}
}
要检测 link 是否被点击,您需要添加 target="_blank"
:
<a href="http://www.website.com/50665/3" target="_blank">Next</a>
并捕获此事件:
webControl.ShowCreatedWebView += OnShowNewView;
internal static void OnShowNewView( object sender, ShowCreatedWebViewEventArgs e )
{
// Do sth with your link. it's in e.TargetURL
}
终于可以用传统方式打开了:
System.Diagnostics.Process.Start(e.TargetURL.ToString());
..或者用它做任何你想做的事。您可以找到更多信息 here
我的网站在 div 中有两个 link。 我想点击 div 里面的第二个 link。 这是网站 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>
现在我试图点击包含 Next
的 link。我不想获得 link href
值,我只想单击 link。到目前为止,这是我尝试过的方法:
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));
}
}
要检测 link 是否被点击,您需要添加 target="_blank"
:
<a href="http://www.website.com/50665/3" target="_blank">Next</a>
并捕获此事件:
webControl.ShowCreatedWebView += OnShowNewView;
internal static void OnShowNewView( object sender, ShowCreatedWebViewEventArgs e )
{
// Do sth with your link. it's in e.TargetURL
}
终于可以用传统方式打开了:
System.Diagnostics.Process.Start(e.TargetURL.ToString());
..或者用它做任何你想做的事。您可以找到更多信息 here