0

I would like to simulate the mouse click on a page. I'm using TChromium in Delphi.

I've tried the following code, but it did not work.

code := 'document.getElementById(_2lkdt).click();';
Chromium1.Browser.MainFrame.ExecuteJavaScript(Code, 'about:blank', 0);

The page button is this:

<button class="_2lkdt">
<span data-icon="send" class="">
    <svg id="Layer_1" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="24" height="24">
        <path fill="#263238" fill-opacity=".45" d="M1.101 21.757L23.8 12.028 1.101 2.3l.011 7.912 13.623 1.816-13.623 1.817-.011 7.912z"></path>
    </svg>
</span>

Zhorov
  • 19,993
  • 4
  • 15
  • 38
Wilson Jr
  • 3
  • 1

1 Answers1

2

Solution:

Use document.getElementsByClassName().

Example:

HTML part (button_tchromium.html). I've defined class for testing purpose.

<html>
    <head>
    <meta http-equiv="X-UA-Compatible" content="IE=edge"/>
    <style>
    ._2lkdt {
        border: 1px solid black;
        margin: 25px;
    }
    </style>
    </head>
    <body>

    <button onclick="alert('I am clicked');" class="_2lkdt">
    <span data-icon="send" class="">
        <svg id="Layer_1" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="24" height="24">
            <path fill="#263238" fill-opacity=".45" d="M1.101 21.757L23.8 12.028 1.101 2.3l.011 7.912 13.623 1.816-13.623 1.817-.011 7.912z"></path>
        </svg>
    </span>

    </body>
</html>

DELPHI part:

Just for this test, I use two buttons on a form, one for loading the html file and another for executing javascript. Just check getElementsByClassName() browser support.

procedure TForm1.btnExecuteClick(Sender: TObject);
var
   code: string;
   frame: ICefFrame;
begin
   code := 'var items = document.getElementsByClassName("_2lkdt"); '+
           'for (var i = 0; i < items.length; i++) { ' +
              'if (items[i].nodeName.toLowerCase() === "button") {' +
                 'items[i].click(); ' +
              '}' +
           '}';
   frame := Chromium1.Browser.MainFrame;
   frame.ExecuteJavaScript(code, frame.Url, 0);
end;

Notes:

Tested with Delphi7 and TChromium (Delphi Chromium Embeded, dcef3-2378 branch).

Zhorov
  • 19,993
  • 4
  • 15
  • 38