单击Windows窗体中的CefSharp浏览器中的按钮 [英] Click a button in CefSharp browser in Windows Forms

查看:1659
本文介绍了单击Windows窗体中的CefSharp浏览器中的按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试单击网页(kahoot.it)上的一个按钮,并且我已经知道我可能需要使用Javascript来实现这一目的,只要它与1行JavaScript保持一致即可,因为这很容易在WinForms中实现。我在按钮上没有太多信息,仅

I'm trying to click a button on a webpage (kahoot.it), and I already know that I probably need to use Javascript for that which is fine, as long as it stays with 1 line of JavaScript because that's easy to implement in WinForms. I don't have much information on the button, only:

<button type="submit" value="Submit" class="enter-button__EnterButton-sc-1o9b9va-0 kxpxeu" data-functional-selector="join-game-pin"><span>Enter</span></button>

你们能帮忙吗?页面上只有一个按钮,也许有帮助。

Could you guys please help? There's only one button on the page, maybe that helps.

推荐答案

您需要编写一段JavaScript代码,并在运行时页面已加载。

You need to write a piece of javascript code and run it when the page loaded.

页面加载后运行脚本

要在页面加载后运行代码,您可以使用 ExecuteScriptAsyncWhenPageLoaded 方法,或者您可以处理 FrameLoadEnd LoadingStateChanged

To run the code after the page loaded, you can use ExecuteScriptAsyncWhenPageLoaded method or you can handle FrameLoadEnd or LoadingStateChanged.

DOM操作-查找元素,设置值,单击按钮

对于javascript代码,您可以使用任何可用的javascript函数。例如,使用 getElemenetsByName getElementsByTagName getElementById 查找元素。

For the javascript code, you can use any of the available javascript functions. for example find the element using getElemenetsByName, getElementsByTagName or getElementById.

找到元素后,可以设置其,例如,单击按钮即可通过调用其 click()方法。

After you found the element, you can set its value or for example for a button you can click on it by calling its click() method.

以下代码将 ChromiumWebBrowser 控件添加到 Form 。然后,它浏览 google ,并在搜索框中输入文字,然后点击搜索按钮:

The following code, adds a ChromiumWebBrowser control to a Form. Then it browses google and fill the search box with a text and clicks on the search button:

//using CefSharp;
//using CefSharp.WinForms;
ChromiumWebBrowser browser;
protected override void OnLoad(EventArgs e)
{
    base.OnLoad(e);
    browser = new ChromiumWebBrowser("https://www.google.com/");
    browser.Dock = DockStyle.Fill;
    Controls.Add(browser);
    var script = @"
            document.getElementsByName('q')[0].value = 'CefSharp C# Example';
            document.getElementsByName('btnK')[0].click();
        ";
    browser.ExecuteScriptAsyncWhenPageLoaded(script);
}

示例2

在下面的示例中,使用 ExecuteScriptAsync ,您可以在搜索框中填充文本,然后以编程方式单击搜索按钮:

In the following example, using ExecuteScriptAsync you can fill the search box with a text and clicks on the search button programmatically:

//using CefSharp;
//using CefSharp.WinForms;
ChromiumWebBrowser browser;
protected override void OnLoad(EventArgs e)
{
    base.OnLoad(e);
    browser = new ChromiumWebBrowser("https://www.google.com/");
    browser.Dock = DockStyle.Fill;
    Controls.Add(browser);
}
private void button1_Click(object sender, EventArgs e)
{
    var script = @"
        document.getElementsByName('q')[0].value = 'CefSharp C# Example';
        document.getElementsByName('btnK')[0].click();
    ";
    browser.ExecuteScriptAsync(script);
}

注意: 对于您的情况,对于 kahoot.it ,脚本应为:

var script = @"
        document.getElementById('game-input').value = '123';
        document.getElementsByTagName('button')[0].click();
    ";

这篇关于单击Windows窗体中的CefSharp浏览器中的按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆