Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

232
Vistas
CefSharp Running JavaScript

I am having difficulty running JS in CefSharp due to the command running straight after each other. Basically, I need a wait command to await the page to load after executing the last script.

Hence it would be:

await Browser.ExecuteScriptAsync(script1);
Browser.WaitForLoad();
Task<JavascriptResponse> test = await Browser.EvaluateScriptAsync(script2);

I have found a few resources but I'm only new to programming so I'm finding it a bit difficult to work out how I can do what I want.

using System.Threading;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using CefSharp;
using CefSharp.Wpf;

namespace WpfApp2
{
    public partial class MainWindow : Window
    {
        private string MessagesInbox;
        public MainWindow()
        {
            InitializeComponent();
        }


        private void Browser_Loaded(object sender, RoutedEventArgs e)
        {
            Browser.LoadingStateChanged += async (Sender, args) => {if (args.IsLoading == false)
                {
                    await Browser.EvaluateScriptAsync(@"document.getElementById('User').value = 'test'; document.getElementById('Password').value = 'password'; document.getElementById('LoginButton').click();");
                }};
            Task<JavascriptResponse> test = Browser.EvaluateScriptAsPromiseAsync(@"return new Promise(function(resolve, reject) { setTimeout(resolve.bind(null, { var test = ''; document.querySelectorAll('.MasterAction').forEach(el => test += el.children[3].children[0].href + ', ')}), 2000);");
            MessagesInbox = test.Result.ToString();
            MessageBox.Show(MessagesInbox);
        }
    }
}
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You should also check the IsLoading for your second method, you can use a bool to track if you are already logged in, and use the second method inside that function aswell like this:

public bool loggedIn = false;
private void Browser_Loaded(object sender, EventArgs e)
    {
        Browser.LoadingStateChanged += async (Sender, args) =>
        {
            if (args.IsLoading == false)
            {
                if (!loggedIn)
                {
                    await Browser.EvaluateScriptAsync(@"document.getElementById('User').value = 'test'; document.getElementById('Password').value = 'password'; document.getElementById('LoginButton').click();")
                        .ContinueWith(x =>
                        {
                            var response = x.Result;
                            System.Diagnostics.Debug.WriteLine(response.Result);
                            if (response.Result == null)
                            {
                                System.Diagnostics.Debug.WriteLine("should be changed to true");
                                loggedIn = true;
                            }
                        });
                }
                else
                {
                    JavascriptResponse test = await Browser.EvaluateScriptAsync(@"var test = ''; document.querySelectorAll('.MasterAction').forEach(el => test += el.children[3].children[0].href + ', ');test.substr(0,test.length-2)");
                    MessagesInbox = test.Result.ToString();
                    System.Diagnostics.Debug.WriteLine(MessagesInbox);
                }
            }
        };
    }
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda