Is it possible to ExecuteJavaScript or emulate user actions like key pressing on loaded page? #235

Closed
opened 2026-01-29 16:34:25 +00:00 by claunia · 9 comments
Owner

Originally created by @ziomyslaw on GitHub (Nov 14, 2018).

I am looking for solution to replace NightmareJs with c# implementation.
The NightmareJs wraps electron api to allows not only to type or click but execute js as well i.e.
win.webContents.executeJavaScript(src)
WebContents does not provide such methods.
Is there any equivalent way to do this?

Originally created by @ziomyslaw on GitHub (Nov 14, 2018). I am looking for solution to replace NightmareJs with c# implementation. The NightmareJs wraps electron api to allows not only to type or click but execute js as well i.e. [win.webContents.executeJavaScript(src)](https://github.com/segmentio/nightmare/blob/25467e5a1071c05dcbce7f58e137f89c4b8fdd93/lib/runner.js#L395) `WebContents` does not provide such methods. Is there any equivalent way to do this?
claunia added the help wanted label 2026-01-29 16:34:25 +00:00
Author
Owner

@kwakuduahc1 commented on GitHub (Nov 18, 2018):

Are you using an SPA framework or not?

@kwakuduahc1 commented on GitHub (Nov 18, 2018): Are you using an SPA framework or not?
Author
Owner

@ziomyslaw commented on GitHub (Nov 19, 2018):

I used to use NightmareJs + MeteorJs, but I'm looking for c# solution. Something faster than Selenium+Chrome.

@ziomyslaw commented on GitHub (Nov 19, 2018): I used to use NightmareJs + MeteorJs, but I'm looking for c# solution. Something faster than Selenium+Chrome.
Author
Owner

@GregorBiswanger commented on GitHub (Jan 3, 2019):

@ziomyslaw you can use https://electronjs.org/spectron

For .NET Support write a Spectron.NET solution for Electron.NET :)

@GregorBiswanger commented on GitHub (Jan 3, 2019): @ziomyslaw you can use https://electronjs.org/spectron For .NET Support write a Spectron.NET solution for Electron.NET :)
Author
Owner

@niklr commented on GitHub (Feb 24, 2019):

I think what he means is similar to this question: https://stackoverflow.com/questions/28920621/how-to-call-a-javascript-function-on-a-web-page-rendered-by-electron

What is the equivalent to the following solution in Electron.NET?

app.on('ready', function() {

  ...

  mainWindow.webContents.on('did-finish-load', function() {
    mainWindow.webContents.executeJavaScript("alert('Hello There!');");
  });

  ...

});
@niklr commented on GitHub (Feb 24, 2019): I think what he means is similar to this question: https://stackoverflow.com/questions/28920621/how-to-call-a-javascript-function-on-a-web-page-rendered-by-electron What is the equivalent to the following solution in Electron.NET? ``` app.on('ready', function() { ... mainWindow.webContents.on('did-finish-load', function() { mainWindow.webContents.executeJavaScript("alert('Hello There!');"); }); ... }); ```
Author
Owner

@LittleEndu commented on GitHub (Oct 26, 2019):

Is there any solution to this? Being able to execute javascript and to convert the resulting javascript object into a C# object would be very useful.

@LittleEndu commented on GitHub (Oct 26, 2019): Is there any solution to this? Being able to execute javascript and to convert the resulting javascript object into a C# object would be very useful.
Author
Owner

@GregorBiswanger commented on GitHub (Nov 4, 2019):

@LittleEndu yes, that works with the ElectronHost Hook feature. Take a look at the example of the WebApp here. There is a sample code deposited. In the example application you can also get a step by step guide.

@GregorBiswanger commented on GitHub (Nov 4, 2019): @LittleEndu yes, that works with the **ElectronHost Hook feature**. Take a look at the example of the WebApp here. There is a sample code deposited. In the example application you can also get a step by step guide.
Author
Owner

@MrCircuit commented on GitHub (Jan 13, 2020):

I managed to inject the "electron-chromedriver" npm package into the build command of ElectronNET. It was simply by adding another line after npm install at line 123 in BuildCommand.cs:

            ProcessHelper.CmdExecute("npm install electron-chromedriver", tempPath);

Of course, this could also be achieved by editing the packages.json file.

Using this modified build command, I built the whole ElectronNet project. Then created a small .NET test project with ChromeDriver that uses the "BinaryLocation" property at driver creation. With this setup, I can control and test the ElectronNET application.

@MrCircuit commented on GitHub (Jan 13, 2020): I managed to inject the "electron-chromedriver" npm package into the build command of ElectronNET. It was simply by adding another line after npm install at line 123 in BuildCommand.cs: ProcessHelper.CmdExecute("npm install electron-chromedriver", tempPath); Of course, this could also be achieved by editing the packages.json file. Using this modified build command, I built the whole ElectronNet project. Then created a small .NET test project with ChromeDriver that uses the "BinaryLocation" property at driver creation. With this setup, I can control and test the ElectronNET application.
Author
Owner

@vleeuwenmenno commented on GitHub (Aug 19, 2020):

I am trying to execute some JS code as well, but so far I have not found any solution. I am in need of the executeJavaScript(string) function.

@vleeuwenmenno commented on GitHub (Aug 19, 2020): I am trying to execute some JS code as well, but so far I have not found any solution. I am in need of the `executeJavaScript(string)` function.
Author
Owner

@windhuan commented on GitHub (Aug 27, 2020):

i used LoadURL to exec javascript .

            browserWindow.WebContents.OnDidFinishLoad += () =>
            {
                browserWindow.LoadURL("javascript:alert('111')");
            };
@windhuan commented on GitHub (Aug 27, 2020): i used LoadURL to exec javascript . browserWindow.WebContents.OnDidFinishLoad += () => { browserWindow.LoadURL("javascript:alert('111')"); };
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/Electron.NET#235