背景
页面有两个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())){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;