热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

javafx如何找到webview中的iframe内容

背景页面有两个iframe,iframe的属性都一致,需要获取第一个iframe,最终获取到里面的节点内容。注意,如果通过

背景

页面有两个iframe,iframe的属性都一致,需要获取第一个iframe,最终获取到里面的节点内容。注意,如果通过iframe需要定位到dom元素进行模拟点击,看是否可以直接通过iframe的src,直接通过webview浏览器进行加载定位

转化为文本格式

Document doc &#61; webview.getEngine().getDocument();NodeList bodys &#61; doc.getElementsByTagName("iframe");int countt&#61;0;for(int i&#61;0;i<bodys.getLength();i&#43;&#43;){Node bodyitem &#61; bodys.item(i);NamedNodeMap bodyitemAttribute &#61; bodyitem.getAttributes();Node itemname &#61; bodyitemAttribute.getNamedItem("class");if(itemname!&#61;null){if ("ke-edit-iframe".equals(itemname.getTextContent())){//这步很重要&#xff0c;将node转换成HTMLIFRAMEELEMENT或者您需要的HTMLINPUTELEMENT等HTMLIFrameElement itemname1 &#61; (HTMLIFrameElement) bodyitem;Document iframeContentDoc &#61; itemname1.getContentDocument();Element rootElement &#61; iframeContentDoc.getDocumentElement();NodeList rootElement &#61;iframeContentDoc.getDocumentElement().getElementsByTagName("body");//结果System.out.println(rootElement.item(0).getTextContent());}else{System.out.println("not found");}}}

转换为html格式代码打印到console

Document doc &#61; webview.getEngine().getDocument();NodeList bodys &#61; doc.getElementsByTagName("iframe");int countt&#61;0;for(int i&#61;0;i<bodys.getLength();i&#43;&#43;){Node bodyitem &#61; bodys.item(i);NamedNodeMap bodyitemAttribute &#61; bodyitem.getAttributes();Node itemname &#61; bodyitemAttribute.getNamedItem("class");if(itemname!&#61;null){if ("ke-edit-iframe".equals(itemname.getTextContent()) && (i&#61;&#61;0)){HTMLIFrameElement itemname1 &#61; (HTMLIFrameElement) bodyitem;Document iframeContentDoc &#61; itemname1.getContentDocument();NodeList rootElement &#61; iframeContentDoc.getDocumentElement().getElementsByTagName("body");Transformer transformer &#61; null;try {transformer &#61; TransformerFactory.newInstance().newTransformer();} catch (TransformerConfigurationException e) {e.printStackTrace();}transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");transformer.setOutputProperty(OutputKeys.METHOD, "html");transformer.setOutputProperty(OutputKeys.INDENT, "yes");transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");try {transformer.transform(new DOMSource(rootElement.item(0)),new StreamResult(new OutputStreamWriter(System.out, "UTF-8")));} catch (TransformerException e) {e.printStackTrace();} catch (UnsupportedEncodingException e) {e.printStackTrace();}rootElement.item(0).setTextContent("cccccccccc");}else{System.out.println("not found");}}}

转换为html格式到字符串

Document doc &#61; webview.getEngine().getDocument();NodeList bodys &#61; doc.getElementsByTagName("iframe");int countt&#61;0;for(int i&#61;0;i<bodys.getLength();i&#43;&#43;){Node bodyitem &#61; bodys.item(i);NamedNodeMap bodyitemAttribute &#61; bodyitem.getAttributes();Node itemname &#61; bodyitemAttribute.getNamedItem("class");if(itemname!&#61;null){if ("ke-edit-iframe".equals(itemname.getTextContent()) && i&#61;&#61;1){HTMLIFrameElement itemname1 &#61; (HTMLIFrameElement) bodyitem;Document iframeContentDoc &#61; itemname1.getContentDocument();NodeList rootElement &#61; iframeContentDoc.getDocumentElement().getElementsByTagName("body");Transformer transformer &#61; null;try {transformer &#61; TransformerFactory.newInstance().newTransformer();} catch (TransformerConfigurationException e) {e.printStackTrace();}transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");transformer.setOutputProperty(OutputKeys.METHOD, "html");transformer.setOutputProperty(OutputKeys.INDENT, "yes");transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");ByteArrayOutputStream bos &#61; new ByteArrayOutputStream();try {transformer.transform(new DOMSource(rootElement.item(0)),new StreamResult(bos));} catch (TransformerException e) {e.printStackTrace();}System.out.println(bos.toString());}else{System.out.println("not found");}}}&#96;&#96;&#96;


推荐阅读
author-avatar
mobiledu2502912677
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有