在winforms webbrowser control

时间:2016-01-11 03:28:01

标签: javascript c# winforms webbrowser-control

我正在尝试从我的应用程序中自动登录其中一个在线浏览器游戏。但是,当我检查登录按钮时,我看到了这个HTML代码。

<div id="js_login_button">
                <a href="#" onclick="$('#login_submit_button').click()" class="login_button">
                    <span class="button_left"></span>
                    <span class="button_middle">Login</span>
                    <span class="button_right"></span>
                </a>
            </div>  

由于我无法触及该按钮的ID,我无法使用

webbrowser.Document.GetElementById("button_id").InvokeMember("click");

如何调用这些按钮?

编辑:想知道哪个游戏是谁: https://www.tribalwars.net/

<form action="index.php?action=login&amp;show_server_selection=1" method="post" id="login_form" onsubmit="return Index.login_submit();" data-tracked="true">
    <div>
        <label for="user">
            <strong>User name:</strong>
            <span>
                <input id="user" name="user" class="text" type="text" value="" onkeydown="if((e=window.event||event) &amp;&amp; e.keyCode == 13 &amp;&amp; $('#user').val() &amp;&amp; $('#password').val()) $('#login_form').submit()" data-tracked="true">
            </span>
        </label>
        <label for="password">
            <strong>Password:</strong>
            <span>
                <input name="clear" type="hidden" value="true" data-tracked="true">
                <input id="password" name="password" class="text" type="password" onkeydown="if((e=window.event||event) &amp;&amp; e.keyCode == 13 &amp;&amp; $('#user').val() &amp;&amp; $('#password').val()) $('#login_form').submit()" data-tracked="true">
            </span>
        </label>

        <input type="submit" id="login_submit_button" style="display: none" data-tracked="true">

        <label for="cookie" class="remember_me">
            <input id="cookie" type="checkbox" name="cookie" value="true" checked="checked" data-tracked="true">
                Remember me
        </label>

        <div id="login-buttons">
                            <a id="fb-login-button" class="social-login-button" href="https://ipp-facebook.innogames.de/?game=staemme&amp;market=en&amp;" title="Play with Facebook"></a>

            <div id="js_login_button">
                <a href="#" onclick="$('#login_submit_button').click()" class="login_button">
                    <span class="button_left"></span>
                    <span class="button_middle">Login</span>
                    <span class="button_right"></span>
                </a>
            </div>          
        </div>

        <br style="clear:both;">
    </div>
</form>

在此页面上,名为登录的按钮具有相同的属性。

编辑2:这是显示我尝试(和失败)的源代码:

public Form1()
{
    InitializeComponent();
    browser.Navigate("https://www.tribalwars.net/"); // browser is defined in the form as a WebBrowser element
}

private void btnTest_Click(object sender, EventArgs e)
{

    //var debug = browser.Document.GetElementById("login_submit_button").InvokeMember("click"); // there is a runtime null referance error here

    var elements = browser.Document.GetElementsByTagName("Login");

    foreach (HtmlElement element in elements)
    {
        // The program does not enter here, there is no element !
        //element.InnerHTML to see if it's the one you want
        element.InvokeMember("click");

    }
}

1 个答案:

答案 0 :(得分:2)

伪造点击"button_middle"范围标记应该有效;我希望点击事件会冒泡到其父级a标记,该标记有一个提交表单的onclick事件。

var elements = browser.Document.GetElementsByTagName("span");

foreach (HtmlElement element in elements)
{
    // If there's more than one button, you can check the
    //element.InnerHTML to see if it's the one you want
    if (element.InnerHtml != null && element.InnerHtml.Contains("Login"))
    {
        element.InvokeMember("click");
    }
}