Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

233
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda