0

Iam using CefSharp winform i try login to site ..

but this site using ajax which mean the site page not reload to get source

so how i can know if AJAX is completed ?

i need something like that's or any other Solution

           var cvbar = chrom.EvaluateScriptAsync("Is AJAX completed ??")
           var response = cvbar.Result;
          if (response.Success == true && response.Result.ToString() !=   "")
          {
          //  MessageBox.Show(response.Result.ToString());
          }


        var response1 = cvbar1.Result;
        string HTML = await chrom.GetSourceAsync();
Martin
  • 17
  • 3
  • Not matter it is using AJAX or not, there must be a URL/entry point for user authentication. Try to post all credential data to that entry point and you should get the response, either valid or something else. From that, do what you want implement. – wannadream Nov 03 '18 at 16:01
  • iam try login to this page if login not success will be label "User Name Or passord in valid" but thats not happen unless ajax completed so i how i can know "if ajax completed " then i check the source of page – Martin Nov 03 '18 at 16:05
  • Maybe I'm missing something + I've only toyed w/CefSharp a bit in the past so not sure this is an orthodox solution but have you tried binding an object in javascript? You could then invoke your c# delegate from the javascript one: https://stackoverflow.com/questions/23425059/call-net-from-javascript-in-cefsharp-1-wpf – blins Nov 03 '18 at 16:06
  • Can you set a timer to wait for, e.g. 20 seconds? After the XMLHttpRequest is supposed to finish its job, check back the response of current page again. Or you need to study the JS code of the page and find out which JS object has been changed, then you can use EvaluateScriptAsync to assess the state. – wannadream Nov 03 '18 at 16:25
  • @wannadream See my link , would that not be a valid mechanism to relay the response? Then it would not need to be timer-based and has the advantage of just doing it the way you suggest in your first comment. – blins Nov 03 '18 at 16:38
  • @Martin Depending on a label in order to determine success/failure of a request sounds like a bad idea. Assuming you have control over the js code that is performing the request then, as wannadream describes in the first comment, you should (ideal) handle the response and check the actual status code there. At that point, from your script, you should be able to call back to c# as the answer I linked to above describes. – blins Nov 03 '18 at 16:56

1 Answers1

0

generate js script

  async function eventClick() {   
  let target = (document.evaluate(".//html", document, null,
  XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue);
  let config = { attributes: true, childList: true, subtree: true, characterData: true + };
  let observer;

    function isItDone() {
      return new Promise(resolve => {
          // wait ajax response
          setTimeout(() => { resolve(false); }, 30000); 
          // catch ajax response
          observer = new MutationObserver(() => { 
              if (!compareObjects(oldElements, mutateElements) 
                    resolve(true);
          }
        });
        observer.observe(target, config);
    });
  }
  // ajax request
  document.evaluate(".//button", document.body, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue.click(); 
  let change = await isItDone();
  console.log(change);
  observer.disconnect();
  observer = null; target = null; config = null; 
  return change;  
  }

exequte in cefsarp

JavascriptResponse responseClick = await browser.EvaluateScriptAsPromiseAsync("return eventClick();");

Elgin Maksim
  • 31
  • 1
  • 10