Note: Advice in this article will only work for JxBrowser 6. See the corresponding article for JxBrowser 7 here.(注意:本文中的建议仅适用于JxBrowser6,JxBrowser7相应文章请点击这里。)


JxBrowser DOM API provides functionality that allows you to simulate click on any HTML element on the loaded web page. To simulate click you need to make sure that web page is loaded completely and find the required HTML element in document: (JxBrowser DOM API提供了允许您模拟在加载的网页上的任何HTML元素上单击的功能。要模拟点击,您需要确保网页已完全加载,并在文档中找到所需的HTML元素:)

DOMDocument document = browser.getDocument();
DOMElement link = document.findElement(By.tagName("button"));

Once you get the reference to the required HTML element, you can simulate click using the DOMNode.click() method. (一旦获得对所需HTML元素的引用,就可以使用DOMNode.click()方法来模拟点击。)

link.click();

Please note that this method works asynchronously. When this method returns, it doesn't mean that click simulation is finished.(请注意,此方法异步工作。返回此方法时,并不表示点击模拟已完成。)

This functionality can be useful in automated testing when you need to simulate user actions including mouse clicks on specified HTML elements.(当您需要模拟用户操作(包括在指定的HTML元素上单击鼠标)时,此功能在自动测试中很有用。)

import com.teamdev.jxbrowser.chromium.Browser;<span class="fr-marker" data-id="0" data-type="false" style="display: none; line-height: 0;"></span><span class="fr-marker" data-id="0" data-type="true" style="display: none; line-height: 0;"></span>
import com.teamdev.jxbrowser.chromium.dom.By;
import com.teamdev.jxbrowser.chromium.dom.DOMDocument;
import com.teamdev.jxbrowser.chromium.dom.DOMElement;
import com.teamdev.jxbrowser.chromium.events.FinishLoadingEvent;
import com.teamdev.jxbrowser.chromium.events.LoadAdapter;
import com.teamdev.jxbrowser.chromium.swing.BrowserView;

import javax.swing.*;
import java.awt.*;

/**
 * This sample demonstrates how to simulate click on a specific HTML element.
 */
public class DOMSimulateClickSample {
    public static void main(String[] args) {
        Browser browser = new Browser();
        BrowserView browserView = new BrowserView(browser);

        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.add(browserView, BorderLayout.CENTER);
        frame.setSize(800, 600);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);

        browser.addLoadListener(new LoadAdapter() {
            @Override
            public void onFinishLoadingFrame(FinishLoadingEvent event) {
                if (event.isMainFrame()) {
                    Browser browser = event.getBrowser();
                    DOMDocument document = browser.getDocument();
                    DOMElement link = document.findElement(By.tagName("button"));
                    if (link != null) {
                        link.click();
                    }
                }
            }
        });
        browser.loadHTML("<html><body><button id='button' " +
                "onclick=\"alert('Button has been clicked!');\">Click Me!</button>" +
                "</body></html>");
    }
}