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; }
tempPage = domElement.click(); Thread.sleep(500); DomElement surge_url = tempPage.getElementById("surge_url"); if (surge_url != null) { String href = surge_url.getAttribute("href");
WebClient wc = new WebClient(BrowserVersion.CHROME); HtmlPage p = wc.getPage(url) ((HtmlTextInput) p.getElementById(userNameId)).setText(userName); ((HtmlTextInput) p.getElementById(passId)).setText(pass); p = ((HtmlElement) p.getElementById(submitBtnId)).click(); // Just as an example for something I've had to do, I use // UnexpectedPage when the "content-type" is "application/zip" UnexpectedPage up = ((HtmlElement) p.getElementById(downloadBtn)).click(); InputStream in = up.getInputStream(); ...
public String getStyleClass(String elementId) { HtmlElement element = (HtmlElement)currentPage.getElementById(elementId); checkState(element != null, "Cannot find element: " + elementId); return element.getAttribute("class"); }
protected String getValue(HtmlPage htmlPage, String id, boolean outputText) { if(outputText) { return htmlPage.getElementById(id).getTextContent(); } return htmlPage.getElementById(id).getAttribute("value"); }
... HtmlPage page = (HtmlPage) webClient.getPage(url); HtmlInput details = null; int tries = 10; while (tries > 0 && details == null) { tries--; details = (HtmlInput) page.getElementById("PlaceHolderConteudo_grdListaResultado_btnDetalhar_0"); synchronized (page) { page.wait(1500); } } ...
HtmlPage page2= (HtmlPage) webClient.getPage("https://controlcenter-itv2.centurylink.com/business/controlcenter/ordering/initiate-orders-whsl/batch"); System.out.println(page2.asXml()); HtmlForm form1 =(HtmlForm) page2.getElementById("batchOrderForm");
/** * Returns the HTML element with the specified ID. If more than one element * has this ID (not allowed by the HTML spec), then this method returns the * first one. * * @param elementId the ID value to search for * @param <E> the element type * @return the HTML element with the specified ID * @throws ElementNotFoundException if no element was found matching the specified ID */ @SuppressWarnings("unchecked") public <E extends HtmlElement> E getHtmlElementById(final String elementId) throws ElementNotFoundException { final DomElement element = getElementById(elementId); if (element == null) { throw new ElementNotFoundException("*", "id", elementId); } return (E) element; }
public void clickElementWithId(String id) { HtmlElement element = (HtmlElement)currentPage.getElementById(id); checkState(element != null, "Cannot find element: " + id); try { element.click(); waitForAsyncEventsThatBeginWithinDefaultTime(); } catch (IOException e) { throw new RuntimeException(e); } }
WebClient webClient = new WebClient(BrowserVersion.CHROME);\ HtmlPage page = webClient.getPage("http://link.com/"); webClient.waitForBackgroundJavaScript(3000); DomElement el = page.getElementById("_eF");
import com.gargoylesoftware.htmlunit.WebClient; import com.gargoylesoftware.htmlunit.html.HtmlPage; public class Main { public static void main(String[] args) throws Exception { final WebClient webClient = new WebClient(); final HtmlPage page = webClient.getPage("http://www.mp3juices.cc/download/sEhy-RXkNo0/mp3/rihanna_-_man_down/"); webClient.closeAllWindows(); System.out.println(page.getElementById("download_link", false).getAttribute("href")); } }
public SignupPage(HtmlPage signupPage) { this.signupPage = signupPage; assertNotNull("The sign up page has a username field.", this.signupPage.getElementById("username")); for (HtmlForm signupForm : this.signupPage.getForms()) { if (signupForm.getInputsByName("username").size() == 0) continue; this.signupForm = signupForm; } }
try (final WebClient webClient = new WebClient(BrowserVersion.CHROME)) { HtmlPage page = webClient.getPage("http://www.google.com"); HtmlAnchor a = page.getHtmlElementById("gb_70"); page = a.click(); HtmlForm form = (HtmlForm) page.getElementById("gaia_loginform"); HtmlTextInput email = (HtmlTextInput) page.getElementById("Email"); email.setValueAttribute(myEmailAddress); HtmlSubmitInput nextButton = page.getHtmlElementById("next"); System.out.println(nextButton.asXml()); HtmlPage newWebpage = nextButton.click(); String status = newWebpage.getWebResponse().getStatusMessage(); System.out.println(status); }
protected void setInputValue(HtmlPage htmlPage, HtmlForm htmlForm, String inputId, String value) { try { htmlForm.getInputByName(inputId).setValueAttribute(value); } catch (ElementNotFoundException e) { ((HtmlInput)htmlPage.getElementById(inputId)).setValueAttribute(value); } }
public void checkComponentSource(URL pageName, String xmlunitPage, By pageElementToTest) throws IOException, SAXException { WebClient client = new WebClient(); client.getOptions().setJavaScriptEnabled(false); HtmlPage page = client.getPage(pageName); DomElement element; String locator = pageElementToTest.toString(); locator = locator.substring(locator.indexOf(':') + 1).trim(); if (pageElementToTest instanceof ById) { element = page.getElementById(locator); } else if (pageElementToTest instanceof ByTagName) { element = page.getElementsByTagName(locator).get(0); } else { throw new IllegalArgumentException("Only id and name are supported"); } String pageCode = element.asXml(); checkXmlStructure(xmlunitPage, pageCode); }
@Test(groups = INTEGRATION) @SpecAssertion(section = CONVERSATION_CONTEXT_EE, id = "ha") @SpecAssertion(section = CONVERSATION, id = "j") public void testConversationIdMayBeSetByApplication() throws Exception { WebClient client = new WebClient(); HtmlPage page = client.getPage(getPath("cumulus.jsf")); assertFalse(isLongRunning(page)); // begin a conversation HtmlSubmitInput beginConversationButton = (HtmlSubmitInput) page .getElementById("form:beginConversationIdentifiedByCustomIdentifier"); page = beginConversationButton.click(); assertTrue(isLongRunning(page)); assertEquals(getCid(page), "humilis"); assertTrue(page.getBody().getTextContent().contains("Cumulus humilis")); HtmlPage nextPage = client.getPage(getPath("cumulus.jsf?cid=humilis")); assertTrue(isLongRunning(nextPage)); assertEquals(getCid(page), "humilis"); }
final WebClient webClient = new WebClient(); final HtmlPage page1 = webClient.getPage("http://www.facebook.com"); final HtmlForm form = (HtmlForm) page1.getElementById("login_form"); final HtmlSubmitInput button = (HtmlSubmitInput) form.getInputsByValue("Log In").get(0); final HtmlTextInput textField = form.getInputByName("email"); textField.setValueAttribute("jon@jon.com"); final HtmlPasswordInput textField2 = form.getInputByName("pass"); textField2.setValueAttribute("ahhhh"); final HtmlPage page2 = button.click();
WebClient webClient = new WebClient(BrowserVersion.CHROME); webClient.getOptions().setJavaScriptEnabled(true); webClient.getOptions().setThrowExceptionOnScriptError(false); webClient.getOptions().setThrowExceptionOnFailingStatusCode(false); HtmlPage page = webClient.getPage("https://scholar.google.com/"); HtmlInput searchBox = (HtmlInput) page.getElementById("gs_hp_tsi"); searchBox.setValueAttribute("internet of things for smart cities"); HtmlButton googleSearchSubmitButton = page.getElementByName("btnG"); page = googleSearchSubmitButton.click(); HtmlAnchor anchor = page.getAnchorByText("Cite"); anchor.click(); webClient.waitForBackgroundJavaScript(5000); HtmlAnchor linkBibTex = page.getAnchorByText("BibTeX"); TextPage neededPage = linkBibTex.click(); System.out.println(neededPage.getContent()); webClient.close();
@Test @RunAsClient public final void testSatisfied() throws Exception { UserAgent ua = assertInitialPage(); HtmlPage page = ua.getHomePage(); // Script HtmlAnchor trigger = (HtmlAnchor)page.getElementById("trigger"); trigger.click(); List<String> alerts = ua.getAlerts(page); assertEquals(Arrays.asList("OK MEN"), alerts); // CSS DomNodeList<DomElement> links = page.getElementsByTagName("link"); assertEquals(1, links.size()); HtmlLink link = (HtmlLink)links.get(0); assertTrue(link.getHrefAttribute().endsWith("main.css")); } }
@Test @RunAsClient public final void testSatisfied() throws Exception { UserAgent ua = assertInitialPage(); HtmlPage page = ua.getHomePage(); // Script HtmlAnchor trigger = (HtmlAnchor)page.getElementById("trigger"); trigger.click(); List<String> alerts = ua.getAlerts(page); assertEquals(Arrays.asList("OK MEN"), alerts); // CSS DomNodeList<DomElement> links = page.getElementsByTagName("link"); assertEquals(1, links.size()); HtmlLink link = (HtmlLink)links.get(0); assertTrue(link.getHrefAttribute().endsWith("main.css")); } }