Search code examples
javascriptdelphichromium-embedded

Delphi CEF4 Chromium - execute javascript action


I am using Delphi and TChromium component to automate some tasks. I have been able to fill forms and submit them by using JavaScript like this:

Chromium1.browser.MainFrame.ExecuteJavaScript
      ('document.getElementById(''LoginForm_username'').value="' +
      Ini.ReadString('config', 'usuario', '') + '"',
      Chromium1.browser.MainFrame.GetURL, 0);

Chromium1.Browser.MainFrame.ExecuteJavaScript('document.forms[0].submit()',
      Chromium1.browser.MainFrame.GetURL, 0);

So far so good. Now i am facing a different problem, perhaps because of my lack of knowledge about JavaScript.

I have a button on this site that in code looks like this:

<form style="border:none;height:16px;width:16px;margin: 0px auto 0px auto;" id="pago-form" action="?r=pago/pago/formpago" method="post">                                  
    <input value="R%0AN%17%9D%C4%D8%AB%A8%27%2A%06S%01%B9.%DF%00c%DC%BFD%B5%D2%E8%C9%A9%C6%84%B7%FFa" 
    name="Pago[idserial]" id="Pago_idserial" type="hidden">                                    
    <input value="R%0AN%17%9D%C4%D8%AB%A8%27%2A%06S%01%B9.%DF%00c%DC%BFD%B5%D2%E8%C9%A9%C6%84%B7%FFa" 
    name="Pago[idplanilla]" id="Pago_idplanilla" type="hidden">                                    
    <input value="K%C7%9B%838d%15V" name="Pago[tipoprocesoagilizacion]" 
    id="Pago_tipoprocesoagilizacion" type="hidden">                                    
    <input value="%F4%1E%D8%FD%BD%ACK3" name="Pago[tipotramiteprocesoagilizacion]" 
    id="Pago_tipotramiteprocesoagilizacion" type="hidden">                                                      
    <input value="%ABv%C1%91%92%E7%D1%3E%28%1A%8E%08%CFi%B2%D9%25k%89%13%B5%5EGT9nb%FDWNa%17" 
    name="Pago[tokenCSRF]" id="Pago_tokenCSRF" type="hidden">
    <input title="Pagar Obtención Express de Pasaporte Electrónico" style="border:none;width:16px;height:16px;margin-left:-20px" 
    src="/themes/saime/imagenes/pay.png" type="image" name="yt0">                            
</form>

Now, if i inspect the button (its a tiny image) it will point me to <input title=Pagar Obtención Express de Pasaporte Electrónico>

The problem is that this is not a button, and it does not have an ID, so i cannot click it by code (I can click it with the mouse). But then, how i could execute this "button" action by code?


Solution

  • I solved this issue by calling using a DOM click like this:

             Chromium1.Browser.MainFrame.ExecuteJavaScript('document.getElementsByName(''yt0'')[0].click();',
          Chromium1.browser.MainFrame.GetURL, 0);
    

    Since the element has no ID, i could find it by name and click it. Its working now =)