TChromium mouse click

拜拜、爱过 提交于 2020-01-06 14:34:45

问题


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>


回答1:


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).



来源:https://stackoverflow.com/questions/50575450/tchromium-mouse-click

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!