te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>javascript - Click a button in CefSharp browser in Windows Forms - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Click a button in CefSharp browser in Windows Forms - Stack Overflow

programmeradmin3浏览0评论

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.

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.

Share Improve this question edited Dec 7, 2019 at 23:01 Reza Aghaei 125k18 gold badges232 silver badges429 bronze badges asked Dec 7, 2019 at 21:08 WoJoWoJo 5161 gold badge4 silver badges21 bronze badges 3
  • 1 Do you mean you want to click on the button which is in the web page, using C# code? – Reza Aghaei Commented Dec 7, 2019 at 21:49
  • Start by reading github./cefsharp/CefSharp/wiki/… – amaitland Commented Dec 7, 2019 at 22:00
  • 1 @RezaAghaei Yes – WoJo Commented Dec 7, 2019 at 22:13
Add a ment  | 

2 Answers 2

Reset to default 10

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

Run script after page loaded

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

DOM manipulation - find element, set value, click on button

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

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.

CefSharp Example - Browse a URL, fill an input and click on a button

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./");
    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);
}

Example 2

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./");
    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);
}

Note: In your case, for kahoot.it, the script should be:

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

Changing Target Framework version to 4.7.2 from 4.0 fixed for me True Target Framework Version

发布评论

评论列表(0)

  1. 暂无评论