HtmlCanvas html = new HtmlCanvas(); html .html() .body() .h1().content("Hello Coder") ._body() ._html(); System.out.println(html.toHtml());
WebClient client = new WebClient(); HtmlPage page = client.getPage("http://stackoverflow.com"); // create a submit button - it doesn't work with 'input' HtmlElement button = page.createElement("button"); button.setAttribute("type", "submit"); // append the button to the form HtmlElement form = ...; form.appendChild(button); // submit the form page = button.click();
@Test public void submittingForm() throws Exception { final WebClient webClient = new WebClient(); // Get the first page final HtmlPage page1 = webClient.getPage("http://some_url"); // Get the form that we are dealing with and within that form, // find the submit button and the field that we want to change. final HtmlForm form = page1.getFormByName("myform"); final HtmlSubmitInput button = form.getInputByName("submitbutton"); final HtmlTextInput textField = form.getInputByName("userid"); // Change the value of the text field textField.setValueAttribute("root"); // Now submit the form by clicking the button and get back the second page. final HtmlPage page2 = button.click(); webClient.closeAllWindows(); }
HtmlPage page3; page3 = webClient.getPage("Website"); HtmlForm loginForm = page3.getFormByName("loginForm"); HtmlTextInput username = loginForm.getInputByName("NameofUsernameElement"); HtmlPasswordInput pass = loginForm.getInputByName("NameofPassowordElement"); HtmlSubmitInput b = loginForm.getInputByValue("LoginButtonValue"); username.setValueAttribute("Actualy Username"); pass.setValueAttribute("Actual Password"); HtmlPage page2; page2 = b.click();
DomNodeList<DomElement> domNodeList = page.getElementsByTagName("a"); return domNodeList.stream() if (domElement.getTextContent().equals("Surge")) { System.out.println(domElement.getTextContent()); return true; try { webClient.waitForBackgroundJavaScript(500); tempPage = domElement.click(); Thread.sleep(500); DomElement surge_url = tempPage.getElementById("surge_url"); if (surge_url != null) { String href = surge_url.getAttribute("href"); System.out.println(href);
WebClient client = ... Page page = client.getPage(url); HtmlSelect select = (HtmlSelect) page.getElementById(mySelectId); HtmlOption option = select.getOptionByValue(desiredOptionValue); select.setSelectedAttribute(option, true);
private ConfigsBean parsePage(HtmlPage page,int id){ DomNode font = page.getElementById("font"+id); if (font == null || !font.asText().equals("可用")){ return null; } DomNode host = page.getElementById("host"+id); DomNode port = page.getElementById("port"+id); DomNode pass = page.getElementById("pass"+id); DomNode encrypt = page.getElementById("encrypt"+id); ConfigsBean configsBean = new ConfigsBean(); configsBean.setRemarks(host.asText()); configsBean.setServer(configsBean.getRemarks()); configsBean.setServer_port(Integer.parseInt(port.asText())); configsBean.setMethod(encrypt.asText()); configsBean.setPassword(pass.asText()); configsBean.setObfs("plain"); configsBean.setId(ModelUtil.generateId()); return configsBean; }
/** * {@inheritDoc} */ @Override public DomNode cloneNode(final boolean deep) { final DomElement clone = (DomElement) super.cloneNode(deep); clone.attributes_ = new NamedAttrNodeMapImpl(clone, isAttributeCaseSensitive()); clone.attributes_.putAll(attributes_); return clone; }
@Override public void runTest(TestRun run) throws IOException { if (++runs == 50) { runs = 0; cleanUp(); init(); } try { page.set(webClient.get().getPage("about:blank")); } catch (IOException e) { throw new RuntimeException(e); } HtmlPage pageRef = page.get(); pageRef.executeJavaScript(readFile(new File(run.getBaseDirectory(), run.getFileName()))); boolean decodeStack = Boolean.parseBoolean(System.getProperty(TeaVMTestRunner.JS_DECODE_STACK, "true")); File debugFile = decodeStack ? new File(run.getBaseDirectory(), run.getFileName() + ".teavmdbg") : null; RhinoResultParser resultParser = new RhinoResultParser(debugFile); AsyncResult asyncResult = new AsyncResult(); Function function = (Function) page.get().executeJavaScript(readResource("teavm-htmlunit-adapter.js")) .getJavaScriptResult(); Object[] args = new Object[] { decodeStack ? createStackDecoderFunction(resultParser) : null, new NativeJavaObject(function, asyncResult, AsyncResult.class) }; pageRef.executeJavaScriptFunction(function, function, args, page.get()); resultParser.parseResult((Scriptable) asyncResult.getResult(), run.getCallback()); }
public static void main(String[] args) throws Exception { // turn off htmlunit warnings java.util.logging.Logger.getLogger("com.gargoylesoftware.htmlunit").setLevel(java.util.logging.Level.OFF); java.util.logging.Logger.getLogger("org.apache.http").setLevel(java.util.logging.Level.OFF); WebClient webClient = new WebClient(); HtmlPage page = webClient.getPage("http://stackoverflow.com"); System.out.println(page.getTitleText()); }
List<HtmlAnchor> articles = (List<HtmlAnchor>) page.getByXPath("//a[@class='lblentrylink']");
private DomNode getFirstChildElement(final DomNode parent) { DomNode node = parent.getFirstChild(); while (node != null && !isAccepted(node)) { node = node.getNextSibling(); } return node; }
/** {@inheritDoc} */ @Override public void startDTD(final String name, final String publicId, final String systemId) { final DomDocumentType type = new DomDocumentType(page_, name, publicId, systemId); page_.setDocumentType(type); final Node child; child = type; page_.appendChild(child); }
/** * Parsing of the children is done, we can move our children to the content. */ @Override protected void onAllChildrenAddedToPage(final boolean postponed) { while (getFirstChild() != null) { final DomNode child = getFirstChild(); child.basicRemove(); domDocumentFragment_.appendChild(child); } } }
/** * {@inheritDoc} */ @Override public DomAttr getAttributeNodeNS(final String namespaceURI, final String localName) { final String qualifiedName = getQualifiedName(namespaceURI, localName); if (qualifiedName != null) { return attributes_.get(qualifiedName); } return null; }
/** * Remove our window also. * {@inheritDoc} */ @Override public void remove() { super.remove(); getEnclosedWindow().close(); } }
private DomNode getNextDomSibling(final DomNode element) { DomNode node = element.getNextSibling(); while (node != null && !isAccepted(node)) { node = node.getNextSibling(); } return node; } }
LiteralControl jsResource = new LiteralControl(); jsResource.Text = "<script type=\"text/javascript\" src=\"js/mini-template-control.js\"></script>"; Page.Header.Controls.Add(jsResource); HtmlLink stylesLink = new HtmlLink(); stylesLink.Attributes["rel"] = "stylesheet"; stylesLink.Attributes["type"] = "text/css"; stylesLink.Href = "css/mini-template-control.css"; Page.Header.Controls.Add(stylesLink);
/** * {@inheritDoc} */ @Override public DisplayStyle getDefaultStyleDisplay() { final HtmlPage page = getHtmlPageOrNull(); if (page.isQuirksMode() && page.hasFeature(HTMLDEFINITION_INLINE_IN_QUIRKS)) { return DisplayStyle.INLINE; } return super.getDefaultStyleDisplay(); } }