如何让Javafx记住表单值?

如何让Javafx记住表单值?,java,webview,javafx,Java,Webview,Javafx,我需要的是用户能够单击网页上的任何标记。单击标记后,一个新按钮出现在网格窗格中。当我在网格窗格中单击“新建”按钮时,会出现一个小对话框,其中包含标签中存在的所有属性 逐步: 我加载google.com 我单击“关于”底部的链接 网格窗格中将显示一个按钮 我单击刚刚出现的按钮 将出现一个较小的窗口,其中列出: 然后我关闭这个较小的窗口并单击“业务” 网格窗格中将显示一个新按钮 我单击刚刚出现的按钮 将显示一个较小的窗口,其中包含新的属性列表: 这就是我的想法 问题是,当我单击Business时

我需要的是用户能够单击网页上的任何标记。单击标记后,一个新按钮出现在
网格窗格中。当我在
网格窗格中单击“新建”按钮时,会出现一个小对话框,其中包含标签中存在的所有属性

逐步:

  • 我加载google.com
  • 我单击“关于”底部的链接
  • 网格窗格中将显示一个按钮
  • 我单击刚刚出现的按钮
  • 将出现一个较小的窗口,其中列出:

  • 然后我关闭这个较小的窗口并单击“业务”

  • 网格窗格中将显示一个新按钮
  • 我单击刚刚出现的按钮
  • 将显示一个较小的窗口,其中包含新的属性列表:
  • 这就是我的想法

    问题是,当我单击Business时,About的
    xpath
    被Business的
    xpath
    替换。当我在gridpane中打开About的内容时,除了xpath现在是
    id(“fsl”)/A[2]
    而不是
    id(“fsl”)/A[3]

    一切都从工人开始

        engine.getLoadWorker().stateProperty().addListener(new ChangeListener<State>(){
            public void changed(ObservableValue ov, State oldState, State newState){
    
                if(newState == javafx.concurrent.Worker.State.SUCCEEDED){
    
                    //attach listener to all tags "*" 
                    EventListener listener = new EventListener(){
    
                        public void handleEvent(org.w3c.dom.events.Event evt) {
                            addNewInstruction(evt);
                            setXPath();
    
                        }
                    };
    
                    setListenerByTagNames(listener, "a");
                }
            }
        });
    
    教学方法

    private void addNewInstruction(org.w3c.dom.events.Event evt){
        String htmlTagName = ((Element)evt.getTarget()).getTagName();
    
        if(instructionIncrementCol == 10){
            instructionIncrementRow++;
            instructionIncrementCol= 0;
        }
    
        final Button button = new Button(htmlTagName);
        button.setOnAction(
                new EventHandler<ActionEvent>() {
    
                    public void handle(ActionEvent event) {
                        //Grid Specs
                        GridPane primaryGrid = new GridPane();
                        Element element = ((Element)evt.getTarget());
                        primaryGrid.setVgap(4);
                        primaryGrid.setPadding(new Insets(5, 5, 5, 5));
    
                        TitledPane titleSlider = new TitledPane();
                        titleSlider.setText("Instructions");
                        titleSlider.setContent(primaryGrid);    
                        titleSlider.setContent(primaryGrid);
    
                        final GridPane secondGrid = new GridPane();
                        secondGrid.setVgap(4);
                        secondGrid.setPadding(new Insets(20));
                        secondGrid.setPrefHeight(100);
    
                        ScrollPane sp = new ScrollPane(secondGrid);
                        sp.setHbarPolicy(ScrollBarPolicy.ALWAYS);
                        sp.setMaxHeight(400);
                        primaryGrid.add(sp, 0, 1);
                        GridPane.setHgrow(sp, Priority.ALWAYS);
                        GridPane.setVgrow(sp, Priority.ALWAYS);
    
                        //Form
                        int rowOnePos = 0;
                        int rowTwoPos = 1;
                        int rowThreePos = 2;
                        int loopRowPos = 3;
    
                        String tagType = element.getTagName();
                        if(tagType.equals("INPUT")){
                            createInputInstructionObject(secondGrid,rowOnePos, element);
                        }else{
                            Button click = new Button("Click");
                            secondGrid.add(click, 0, rowOnePos);
                            click.setOnAction(new EventHandler<ActionEvent>(){
                                public void handle(ActionEvent e){
                                    sql.insertRow("click", element.getAttribute("href"));
                                    //writeToFile("click,"+((Element)evt.getTarget()).getAttribute("href"));
                                }
                            });
                            Text textLabel = new Text("Text");
                            TextField textContentValue = new TextField(element.getTextContent());
                            Button grabDataText = new Button("Grab");
                            textContentValue.setPrefWidth(300);
                            secondGrid.add(textLabel, 0, rowTwoPos);
                            secondGrid.add(textContentValue, 1, rowTwoPos);
                            secondGrid.add(grabDataText, 2, rowTwoPos);
                            textContentValue.setPrefWidth(300);
                        }
    
                        //xpath
                        Text xpathLabel = new Text("XPath");
                        final String stay = bridge.text+" ";
                        TextField xpathValue = new TextField(stay);
                        Button grabDataxpath = new Button("Grab");
    
                        secondGrid.add(xpathLabel, 0, rowThreePos);
                        secondGrid.add(xpathValue, 1, rowThreePos);
                        secondGrid.add(grabDataxpath, 2, rowThreePos);
    
                        //all attributes for this tag
                        NamedNodeMap attributes = element.getAttributes();
    
                        for(int i = 0; i < attributes.getLength(); i++){
                            //create row for each attribute
                            createInstructionObject(attributes, i, secondGrid, loopRowPos);
                        }
    
                        final Stage dialog = new Stage();
                        Scene dialogScene = new Scene(primaryGrid, 500, 500);
                        dialog.setScene(dialogScene);                       
                        dialog.show();
                    }
                 });
    
    
        instructionSecondaryGrid.add(button,instructionIncrementCol,instructionIncrementRow);
        instructionIncrementCol++;
    
    }
    
    private void addNewInstruction(org.w3c.dom.events.Event evt){
    字符串htmlTagName=((元素)evt.getTarget()).getTagName();
    如果(指令增量控制=10){
    指令IncrementRow++;
    指令增量COL=0;
    }
    最终按钮=新按钮(htmlTagName);
    按钮设置动作(
    新的EventHandler(){
    公共无效句柄(ActionEvent事件){
    //网格规格
    GridPane primaryGrid=新建GridPane();
    Element=((Element)evt.getTarget());
    一次网格setVgap(4);
    设置填充(新的插图(5,5,5,5));
    TitledPane titleSlider=新的TitledPane();
    titleSlider.setText(“说明”);
    titleSlider.setContent(主栅格);
    titleSlider.setContent(主栅格);
    final GridPane secondGrid=新GridPane();
    第二个网格。setVgap(4);
    第二个网格。设置填充(新插图(20));
    第二格线高度(100);
    ScrollPane sp=新的ScrollPane(第二个网格);
    sp.setHbarPolicy(ScrollBarPolicy.ALWAYS);
    sp.setMaxHeight(400);
    添加(sp,0,1);
    setHgrow(sp,Priority.ALWAYS);
    setVgrow(sp,Priority.ALWAYS);
    //形式
    int rownepos=0;
    int rowTwoPos=1;
    int ROWTHREPOS=2;
    int loopRowPos=3;
    String tagType=element.getTagName();
    if(tagType.equals(“输入”)){
    createInputInstructionObject(第二个网格、rowOnePos、元素);
    }否则{
    按钮点击=新按钮(“点击”);
    添加(单击,0,rowOnePos);
    click.setOnAction(新的EventHandler(){
    公共无效句柄(ActionEvent e){
    insertRow(“单击”,element.getAttribute(“href”);
    //writeToFile(“单击,”+((元素)evt.getTarget()).getAttribute(“href”);
    }
    });
    文本文本标签=新文本(“文本”);
    TextField textContentValue=新的TextField(element.getTextContent());
    按钮抓取数据文本=新按钮(“抓取”);
    textContentValue.setPrefWidth(300);
    添加(textLabel,0,rowTwoPos);
    添加(textContentValue,1,rowTwoPos);
    添加(grabDataText,2,rowTwoPos);
    textContentValue.setPrefWidth(300);
    }
    //xpath
    Text xpathLabel=新文本(“XPath”);
    最后一个字符串stay=bridge.text+“”;
    TextField xpathValue=新的TextField(停留);
    按钮抓取数据XPath=新按钮(“抓取”);
    添加(xpathLabel,0,rowthrepos);
    添加(xpathValue,1,rowthrepos);
    add(grabDataxpath,2,rowthrepos);
    //此标记的所有属性
    NamedNodeMap attributes=element.getAttributes();
    对于(int i=0;i
    您的代码不太容易理解-许多引用被声明和未使用,或者未声明和引用(后者意味着它不会编译)-但据我所知,您只需要为每个按钮创建一次对话框,而不是每次按下按钮时重新创建:

    private void addNewPane(org.w3c.dom.events.Event evt){
        final Button button = new Button(htmlTagName);
    
        final Stage dialog = new Stage();
        GridPane primaryGrid = new GridPane();
        Element element = ((Element)evt.getTarget());
        primaryGrid.setVgap(4);
        primaryGrid.setPadding(new Insets(5, 5, 5, 5));
    
        TitledPane gridTitlePane = new TitledPane();
        GridPane secondGrid = new GridPane();
        secondGrid.setVgap(4);
        secondGrid.setPadding(new Insets(20));
        secondGrid.setPrefHeight(100);
        ScrollPane sp = new ScrollPane(secondGrid);
        sp.setHbarPolicy(ScrollBarPolicy.ALWAYS);
        sp.setMaxHeight(400);
    
        int rowThreePos = 2;
    
        //text content of data                      
        secondGrid.add(new Text("XPath"), 0, rowThreePos);
        secondGrid.add(new TextField(holdXPath), 1, rowThreePos);
        secondGrid.add(new Button("Grab"), 2, rowThreePos);
    
        primaryGrid.add(sp, 0, 1);
        GridPane.setHgrow(sp, Priority.ALWAYS);
        GridPane.setVgrow(sp, Priority.ALWAYS);
        gridTitlePane.setText("Forms");
        gridTitlePane.setContent(primaryGrid);  
    
        gridTitlePane.setContent(primaryGrid);
        Scene dialogScene = new Scene(primaryGrid, 500, 500);
        dialog.setScene(dialogScene);
    
        button.setOnAction(
                new EventHandler<ActionEvent>() {
                    @Override
                    public void handle(ActionEvent event) {
                        dialog.show();  
                    }
                 });
        SecondaryGrid.add(button,IncrementCol,IncrementRow);
        IncrementCol++;
    }
    
    private void addNewPane(org.w3c.dom.events.Event evt){
    最终按钮=新按钮(htmlTagName);
    最终阶段对话框=新阶段();
    GridPane primaryGrid=新建GridPane();
    Element=((Element)evt.getTarget());
    一次网格setVgap(4);
    设置填充(新的插图(5,5,5,5));
    标题窗格网格标题窗格=ne
    
    private void setXPath(){
        JSObject hrefWindow = (JSObject) engine.executeScript("window");
        hrefWindow.setMember("java", bridge);
        engine.executeScript("var links = document.getElementsByTagName(\"*\");"+
                            "for (var i = 0; i < links.length; i++) {"+
                            "links[i].addEventListener(\"click\", function(){"+
                                "document.onclick = function(event) {" +
                                    "if (event===undefined) event= window.event;" +                     
                                    "var target= 'target' in event? event.target : event.srcElement; " +
    
                                    "var root= document.compatMode==='CSS1Compat'? document.documentElement : document.body;" +
                                    "var mxy= [event.clientX+root.scrollLeft, event.clientY+root.scrollTop];" +
    
                                    "var path= getPathTo(target);" +
                                    "var txy= getPageXY(target);" +
                                    "java.log(path);" +
                                "}" +
                            "});"+
                        "}"+
    
                            "function getPathTo(element) {" +
                                "if (element.id!=='')" +
                                    "return 'id(\"'+element.id+'\")';" +
                                "if (element===document.body)" +
                                    "return element.tagName;" +
    
                                "var ix= 0;" +
                                "var siblings= element.parentNode.childNodes;" +
                                "for (var i= 0; i<siblings.length; i++) {" +
                                    "var sibling= siblings[i];" +
                                    "if (sibling===element)" +
                                        "return getPathTo(element.parentNode)+'/'+element.tagName+'['+(ix+1)+']';" +
                                    "if (sibling.nodeType===1 && sibling.tagName===element.tagName)" +
                                        "ix++;" +
                                "}" +
                            "}" +
    
                            "function getPageXY(element) {" +
                                "var x= 0, y= 0;" +
                                "while (element) {" +
                                    "x+= element.offsetLeft;" +
                                    "y+= element.offsetTop;" +
                                    "element= element.offsetParent;" +
                                "}" +
                                "return [x, y];" +
                            "}");
    }
    
    public class JavaBridge extends Amz
    {
        public String text = "EMPTY STRING";
    
        public void log(String text){
            this.text = text;
        }
    
    }
    
    private void addNewInstruction(org.w3c.dom.events.Event evt){
        String htmlTagName = ((Element)evt.getTarget()).getTagName();
    
        if(instructionIncrementCol == 10){
            instructionIncrementRow++;
            instructionIncrementCol= 0;
        }
    
        final Button button = new Button(htmlTagName);
        button.setOnAction(
                new EventHandler<ActionEvent>() {
    
                    public void handle(ActionEvent event) {
                        //Grid Specs
                        GridPane primaryGrid = new GridPane();
                        Element element = ((Element)evt.getTarget());
                        primaryGrid.setVgap(4);
                        primaryGrid.setPadding(new Insets(5, 5, 5, 5));
    
                        TitledPane titleSlider = new TitledPane();
                        titleSlider.setText("Instructions");
                        titleSlider.setContent(primaryGrid);    
                        titleSlider.setContent(primaryGrid);
    
                        final GridPane secondGrid = new GridPane();
                        secondGrid.setVgap(4);
                        secondGrid.setPadding(new Insets(20));
                        secondGrid.setPrefHeight(100);
    
                        ScrollPane sp = new ScrollPane(secondGrid);
                        sp.setHbarPolicy(ScrollBarPolicy.ALWAYS);
                        sp.setMaxHeight(400);
                        primaryGrid.add(sp, 0, 1);
                        GridPane.setHgrow(sp, Priority.ALWAYS);
                        GridPane.setVgrow(sp, Priority.ALWAYS);
    
                        //Form
                        int rowOnePos = 0;
                        int rowTwoPos = 1;
                        int rowThreePos = 2;
                        int loopRowPos = 3;
    
                        String tagType = element.getTagName();
                        if(tagType.equals("INPUT")){
                            createInputInstructionObject(secondGrid,rowOnePos, element);
                        }else{
                            Button click = new Button("Click");
                            secondGrid.add(click, 0, rowOnePos);
                            click.setOnAction(new EventHandler<ActionEvent>(){
                                public void handle(ActionEvent e){
                                    sql.insertRow("click", element.getAttribute("href"));
                                    //writeToFile("click,"+((Element)evt.getTarget()).getAttribute("href"));
                                }
                            });
                            Text textLabel = new Text("Text");
                            TextField textContentValue = new TextField(element.getTextContent());
                            Button grabDataText = new Button("Grab");
                            textContentValue.setPrefWidth(300);
                            secondGrid.add(textLabel, 0, rowTwoPos);
                            secondGrid.add(textContentValue, 1, rowTwoPos);
                            secondGrid.add(grabDataText, 2, rowTwoPos);
                            textContentValue.setPrefWidth(300);
                        }
    
                        //xpath
                        Text xpathLabel = new Text("XPath");
                        final String stay = bridge.text+" ";
                        TextField xpathValue = new TextField(stay);
                        Button grabDataxpath = new Button("Grab");
    
                        secondGrid.add(xpathLabel, 0, rowThreePos);
                        secondGrid.add(xpathValue, 1, rowThreePos);
                        secondGrid.add(grabDataxpath, 2, rowThreePos);
    
                        //all attributes for this tag
                        NamedNodeMap attributes = element.getAttributes();
    
                        for(int i = 0; i < attributes.getLength(); i++){
                            //create row for each attribute
                            createInstructionObject(attributes, i, secondGrid, loopRowPos);
                        }
    
                        final Stage dialog = new Stage();
                        Scene dialogScene = new Scene(primaryGrid, 500, 500);
                        dialog.setScene(dialogScene);                       
                        dialog.show();
                    }
                 });
    
    
        instructionSecondaryGrid.add(button,instructionIncrementCol,instructionIncrementRow);
        instructionIncrementCol++;
    
    }
    
    private void addNewPane(org.w3c.dom.events.Event evt){
        final Button button = new Button(htmlTagName);
    
        final Stage dialog = new Stage();
        GridPane primaryGrid = new GridPane();
        Element element = ((Element)evt.getTarget());
        primaryGrid.setVgap(4);
        primaryGrid.setPadding(new Insets(5, 5, 5, 5));
    
        TitledPane gridTitlePane = new TitledPane();
        GridPane secondGrid = new GridPane();
        secondGrid.setVgap(4);
        secondGrid.setPadding(new Insets(20));
        secondGrid.setPrefHeight(100);
        ScrollPane sp = new ScrollPane(secondGrid);
        sp.setHbarPolicy(ScrollBarPolicy.ALWAYS);
        sp.setMaxHeight(400);
    
        int rowThreePos = 2;
    
        //text content of data                      
        secondGrid.add(new Text("XPath"), 0, rowThreePos);
        secondGrid.add(new TextField(holdXPath), 1, rowThreePos);
        secondGrid.add(new Button("Grab"), 2, rowThreePos);
    
        primaryGrid.add(sp, 0, 1);
        GridPane.setHgrow(sp, Priority.ALWAYS);
        GridPane.setVgrow(sp, Priority.ALWAYS);
        gridTitlePane.setText("Forms");
        gridTitlePane.setContent(primaryGrid);  
    
        gridTitlePane.setContent(primaryGrid);
        Scene dialogScene = new Scene(primaryGrid, 500, 500);
        dialog.setScene(dialogScene);
    
        button.setOnAction(
                new EventHandler<ActionEvent>() {
                    @Override
                    public void handle(ActionEvent event) {
                        dialog.show();  
                    }
                 });
        SecondaryGrid.add(button,IncrementCol,IncrementRow);
        IncrementCol++;
    }
    
    <a href="site.com" mycustomAttr="1">Click Me</a>
    
        //the JS
        var anchors = document.getElementsByTagName("a"); 
        for (var i = 0; i < anchors.length; i++) {
             java.insertIntoDB(getXPathID(anchors[i]));
             anchors[i].setAttribute("mycustomAttr", java.getMaxId());
         }