javafx-webview加载网页实现模拟button、text、input、textarea、radio自动填充或者点击(通过非id去寻找指定的元素)

46 篇文章 0 订阅

通过getElementsByID找不到的情况下,可以通过nodelist的循环获取到item,进行操作

 @FXML
    void automeetingAction(ActionEvent event) {



        Document doc = webview.getEngine().getDocument();
        NodeList inputNodeList = doc.getElementsByTagName("input");


        for (int i = 0; i < inputNodeList.getLength(); i++) {
            NamedNodeMap attrs = inputNodeList.item(i).getAttributes();

            Node type = attrs.getNamedItem("type");
            String typeStr = "";
            if (type != null) {
                typeStr = attrs.getNamedItem("type").getTextContent();
            }

            Node placeholder = attrs.getNamedItem("placeholder");
            String placeholderStr = "";
            if (placeholder != null) {
                placeholderStr = attrs.getNamedItem("placeholder").getTextContent();
            }

            if (!"".equals(typeStr) && !"".equals(placeholderStr)) {
                if ("text".equals(typeStr) && "议主题".equals(placeholderStr)) {

                    HTMLInputElement buttonThis = (HTMLInputElement) inputNodeList.item(i);
                    try {
                        buttonThis.setValue(getURLParam(webview.getEngine().getLocation(),"problemSummary"));
                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                    }
                }
            }

            //选择外部会议
            if (null != inputNodeList.item(i).getParentNode().getNextSibling() && "会议".equals(inputNodeList.item(i).getParentNode().getNextSibling().getTextContent())) {
                HTMLInputElement buttonThis = (HTMLInputElement) inputNodeList.item(i);
                buttonThis.click();
            }

            //选择立即开始
            if (null != inputNodeList.item(i).getParentNode().getNextSibling() && "立即开始".equals(inputNodeList.item(i).getParentNode().getNextSibling().getTextContent())) {
                HTMLInputElement buttonThis = (HTMLInputElement) inputNodeList.item(i);
                buttonThis.click();
            }

            //选择遵守安全规则
            if (null != inputNodeList.item(i).getParentNode().getNextSibling() && "我承诺遵守以上安全规则".equals(inputNodeList.item(i).getParentNode().getNextSibling().getTextContent())) {
                HTMLInputElement buttonThis = (HTMLInputElement) inputNodeList.item(i);
                buttonThis.click();
            }
        }


        NodeList textareaNodeList = doc.getElementsByTagName("textarea");
        for (int i = 0; i < textareaNodeList.getLength(); i++) {
            NamedNodeMap attrs = textareaNodeList.item(i).getAttributes();


            Node placeholder = attrs.getNamedItem("placeholder");
            String placeholderStr = "";
            if (placeholder != null) {
                placeholderStr = attrs.getNamedItem("placeholder").getTextContent();
            }

            if (!"".equals(placeholderStr)) {
                if ( "请输入外部邮箱".equals(placeholderStr)) {
                    HTMLTextAreaElement buttonThis = (HTMLTextAreaElement) textareaNodeList.item(i);

                    try {
                        buttonThis.setValue(getURLParam(webview.getEngine().getLocation(),"reportContactEmail"));
                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                    }
                }

                if ( "请输入".equals(placeholderStr)) {
                    HTMLTextAreaElement buttonThis = (HTMLTextAreaElement) textareaNodeList.item(i);
                    buttonThis.setValue("lijian 00355334,");
                }
            }

        }

    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值