简体   繁体   中英

Awesomium How to click a div link - .click() does not work (vb.net)

I like to click on a div tag link. Here is the HTML code:

<div class="m7"><div class="mB k7"></div><div class="mB l7"></div></div><span style="-moz-user-select: none;" role="button" class="d-s aj mqa" tabindex="0"><div class="yl kH"></div><div class="dv">Link</div></span>

I used this code on my vb.net Application:

webControl1.ExecuteJavascript("document.getElementsByClassName('dv')[0].click();")

But it does not click and the popup is not opened. When I execute the code in developers console in my firefox browser it works pretty well. So it is not the getElementsByClassName selection that does not work it is Awesomium that does not work here.

Please help me.

Thanks

Cheers

Since the div element doesn't have any click method, we have to add it up. Here's a javascript to add a click method:

function AddClickMethod(element, event) {
   var e = document.createEvent('HTMLEvents'); 
   e.initEvent(event, true, false); 
   element.dispatchEvent(e); 
}

and then to use it in Visual Basic.NET

You just call the javascript function with the parameters document.getElementsByClassName('dv')[0] and click

webControl1.ExecuteJavascript("function AddClickMethod(element, event) {var e = document.createEvent('HTMLEvents'); e.initEvent(event, true, false); element.dispatchEvent(e); } AddClickMethod(document.getElementsByClassName('dv')[0], 'click');")

It is not possible, because Awesomium is outdated. It was a problem of Awesomium.

So result is: switch to cefSharp (chrome browser) or to GeckoFX-45 (Firefox) Bot are free. There is also a further developed Awesomium named DotNetBrowser but it is >1000$ per license.

or try the invokemember method..

 getElementsByClassName("dv")(0).InvokeMember("click")

using my custom function library..

''' <summary>
''' returns a collection of all elements in the document with the specified class name, as a NodeList object.
''' </summary>
''' <param name="className">specified class name</param>
Function getElementsByClassName(ByVal className) As HtmlElement()
    Dim c As New List(Of HtmlElement)
    For Each a As HtmlElement In WebBrowser1.Document.All
        If a.GetAttribute("className").Equals(className) Then
            c.Add(a)
        End If
    Next
    Return c.ToArray
End Function

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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