Friday, May 17, 2024
Homepage · c#
 Popular · Latest · Hot · Upcoming
27
rated 0 times [  32] [ 5]  / answers: 1 / hits: 16419  / 5 Years ago, sat, december 7, 2019, 12:00:00

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.


More From » c#

 Answers
4

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


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


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



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

[#51392] Saturday, November 30, 2019, 5 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
soniap

Total Points: 626
Total Questions: 119
Total Answers: 110

Location: Palestine
Member since Tue, Jul 20, 2021
3 Years ago
;