Search code examples
javascriptc#webbrowser-controlelementawesomium

Awesomium C# | Need Help getting an random Element from many Elements


In this code I got the amount of all persons. This worked. Now I want to get one random person from all person, but this doesn't work. I get an Microsoft.CSharp.RuntimeBinder.RuntimeBinderException at the line using (person) Sorry for my English.

Here the code:

dynamic document = (JSObject)webControl1.ExecuteJavascriptWithResult("document.getElementsByClassName(\"badge\")[1]");
using (document)
{
    string gg = document.innerHTML;
    personcount = Convert.ToInt16(gg);
}

int selectperson = rnd.Next(0, personcount-1);

dynamic document2 = (JSObject)webControl1.ExecuteJavascriptWithResult("document.getElementsByTagName('a')");
using (document2)
{
    dynamic person = document2.getElementByClassName("name")[selectperson];
    using (person)
    {
        MessageBox.Show(person.innerText);
    }
}

and here is the HTML Tag from an example person:

<a class="name" href="http://kontakt-4.dastelefonbuch.de/Neuwied/Martin-Kaiser-Neuwied-Feldkircher-Str.html" rel="nofollow">

Solution

  • I didnt found out how I can do this. But i found another way now everything is working thanks! :)

                    dynamic document = (JSObject)webControl1.ExecuteJavascriptWithResult("document.getElementsByClassName(\"badge\")[1]");
                    string gg = document.innerText;
                    personcount = Convert.ToInt16(gg);
    
                    int odd = rnd.Next(1, personcount * 2);
                    for (int i = 0;odd % 2 == 0;i++)
                    {
                        odd = rnd.Next(1, personcount * 2);
                    }
                    dynamic document2 = (JSObject)webControl1.ExecuteJavascriptWithResult("document.getElementsByClassName('name')["+odd+"]");
                    using (document2)
                    {
                        if (document2.HasMethod("click"))
                        {
                            document2.Invoke("click");
                            stepcount = 2;
                        }
                    }