title: c# Cefsharp how to make correct sequence of JavaScript actions on the web site
body: <p>These sequences of actions work with Thread.Sleep, somewhere in 1 second, somewhere in 2 seconds. I think using Thread.Sleep/Task.Delay is not good. Because it can be performed differently on different computers. How do I execute these sequences without using Thread.Sleep?
  Or it is OK to using Thread.Sleep/Task.Delay?</p>
  <pre><code>        private async void ButtonFind_Click(object sender, EventArgs e)
          {
  
              //Action1
              string jsScript1 = &quot;document.getElementById('story').value=&quot; + '\'' + textFind.Text + '\'';
              await chrome.EvaluateScriptAsync(jsScript1);
  
              //Action2
              string jsScript2 = &quot;document.querySelector('body &gt; div.wrapper &gt; div.header &gt; div.header44 &gt; div.search_panel &gt; span &gt; form &gt; button').click();&quot;;
              await chrome.EvaluateScriptAsync(jsScript2);
  
              //Action3
              Thread.Sleep(1000); //it is necessary to set exactly 1 seconds
              string jsScript3 = &quot;document.getElementsByTagName('a')[2].click();&quot;;
              await chrome.EvaluateScriptAsync(jsScript3);
  
              //Action4
              Thread.Sleep(2000); //it is necessary to set exactly 2 seconds
              string jsScript4 = &quot;document.querySelector('#dle-content &gt; div.section &gt; ul &gt; li:nth-child(3)').click();&quot;;
              await chrome.EvaluateScriptAsync(jsScript4);
          }
  </code></pre>
  <p>I tried to use task expectations, but it didn't help me</p>
  <pre><code>...
  var task4 = chrome.EvaluateScriptAsync(jsScript4);
  task4.Wait();
  </code></pre>
  <p>I also tried to use DOM rendering expectations, which didn't help either</p>
  <pre><code>            string jsScript4 = @&quot;
                    if( document.readyState !== 'loading' ) {
                        myInitCode();
                    } else {
                        document.addEventListener('DOMContentLoaded', function () {
                            myInitCode();
                        });
                    }
  
                    function myInitCode() {
                     var a = document.querySelector('#dle-content &gt; div.section &gt; ul &gt; li:nth-child(3)').click();
                    return a;
                    }
                &quot;;
              
              chrome.EvaluateScriptAsync(jsScript4);
  </code></pre>
  <p>My addition (21.04.2022)</p>
  <hr />
  <p>In third action instead of using Thread.Sleep, im using &quot;While&quot; loop
  Here the algorithm is correct, but for some reason, after pressing the application button, the application is hanging</p>
  <pre><code>                bool test = false;
                  while(test == false)
                  {
                      string myScript = @&quot;
                          (function(){
                              var x = document.getElementsByTagName('a')[1].outerText;
                              return x;
                          })();
                          &quot;;
                      var task = chrome.EvaluateScriptAsync(myScript);
                      task.ContinueWith(x =&gt;
                      {
                          if (!x.IsFaulted)
                          {
                              var response = x.Result;
                              if (response.Success == true)
                              {
                                  var final = response.Result;
                                  if (final.ToString() == textFind.Text)
                                  {
                                      MessageBox.Show(&quot;You found the link&quot;);
                                      test = true;
                                  }
                                  else
                                  {
                                      MessageBox.Show(&quot;You do not found the link&quot;);
                                  }
                              }
                          }
                      }, TaskScheduler.FromCurrentSynchronizationContext());
                  }
  
  </code></pre>
  <p>My addition (23.04.2022)</p>
  <hr />
  <pre><code>string jsScript1 = &quot;document.getElementById('story').value=&quot; + '\'' + textFind.Text + '\'' + &quot;;&quot;
                  + @&quot;
      Promise.resolve()
    .then(() =&gt; document.querySelector('body &gt; div.wrapper &gt; div.header &gt; div.header44 &gt; div.search_panel &gt; span &gt; form &gt; button').click())
    .then(() =&gt;  { var target = document.body;
              const config = { 
                  childList: true, 
                  attributes: true, 
                  characterData: true, 
                  subtree: true, 
                  attributeFilter: ['id'], 
                  attributeOldValue: true, 
                  characterDataOldValue: true 
              }
              const callback = function(mutations) 
              {
                  document.addEventListener('DOMContentLoaded', function(){                    
                  if(document.getElementsByTagName('a')[1].innerText=='Troy')
                      {
                          alert('I got that link');
                      }
                  }, true);
              };
              const observer = new MutationObserver(callback);
              observer.observe(target, config)});
              &quot;;
  
              var task1 = chrome.EvaluateScriptAsPromiseAsync(jsScript1);
              task1.Wait();
  </code></pre>
  <p>Using a MutationObserver wrapped in a promise, using EvaluateScriptAsPromiseAsync to evaluate promise. Also didnt help.
  I came to the conclusion that JavaScript does not save the code when clicking on a search button or after going to another page. How do I save the JavaScript code/request and continue it after clicking on a search button or after going to another page?</p>