HtmlPage currentPage = webClient.getPage("school login site"); HtmlTextInput textField2 = (HtmlTextInput) currentPage.getElementByName("j_username"); textField2.setValueAttribute(email); HtmlTextInput textField3 = (HtmlTextInput) currentPage.getElementByName("j_password"); textField3.setValueAttribute(password);
final HtmlPage page = webClient.getPage("http://www.google.com"); HtmlElement inputField = page.getElementByName("q"); inputField.type("Example input"); HtmlElement btnG = page.getElementByName("btnG"); Page secondPage = btnG.click(); if (secondPage instanceof HtmlPage) { System.out.println(page.getTitleText()); System.out.println(((HtmlPage)secondPage).getTitleText()); }
//Login into Tremor Games and return the client(Saves the cookies). private static WebClient tremorLogin(WebClient webClient) throws Exception { webClient.getOptions().setJavaScriptEnabled(false); HtmlPage currentPage = webClient.getPage("http://www.tremorgames.com/"); //Load page at the STRING address. HtmlInput username = currentPage.getElementByName("loginuser"); //Find element called loginuser for username username.setValueAttribute(user); //Set value for username HtmlInput password = currentPage.getElementByName("loginpassword"); //Find element called loginpassword for password password.setValueAttribute(pass); //Set value for password HtmlSubmitInput submitBtn = currentPage.getElementByName("Submit"); //Find element called Submit to submit form. currentPage = submitBtn.click(); //Click on the button. return webClient; }
HtmlPage page = webClient.getPage("https://www.yourwebsite.com/#"); HtmlAnchor link=page.getElementByName("link"); link.click(); page.refresh();
final WebClient webClient = new WebClient(); HtmlPage page = webClient.getPage("http://www.census.gov/manufacturing/m3/"); HtmlSelect select = (HtmlSelect) page.getElementByName("advanced_xls"); InputStream is = select.setSelectedAttribute("/manufacturing/m3/adv/table2a.xls",true).getWebResponse().getContentAsStream();
import com.gargoylesoftware.htmlunit.*; import com.gargoylesoftware.htmlunit.html.*; public class HtmlUnitFormExample { public static void main(String[] args) throws Exception { WebClient webClient = new WebClient(); HtmlPage page = webClient.getPage("http://www.google.com"); HtmlInput searchBox = page.getElementByName("q"); searchBox.setValueAttribute("htmlunit"); HtmlSubmitInput googleSearchSubmitButton = page.getElementByName("btnG"); // sometimes it's "btnK" page=googleSearchSubmitButton.click(); HtmlDivision resultStatsDiv = page.getFirstByXPath("//div[@id='resultStats']"); System.out.println(resultStatsDiv.asText()); // About 309,000 results webClient.closeAllWindows(); } }
import com.gargoylesoftware.htmlunit.*; import com.gargoylesoftware.htmlunit.html.*; public class HtmlUnitFormExample { public static void main(String[] args) throws Exception { WebClient webClient = new WebClient(); HtmlPage page = webClient.getPage("http://www.google.com"); HtmlInput searchBox = page.getElementByName("q"); searchBox.setValueAttribute("htmlunit"); HtmlSubmitInput googleSearchSubmitButton = page.getElementByName("btnG"); // sometimes it's "btnK" page=googleSearchSubmitButton.click(); HtmlDivision resultStatsDiv = page.getFirstByXPath("//div[@id='resultStats']"); System.out.println(resultStatsDiv.asText()); // About 301,000 results webClient.closeAllWindows(); } }
WebClient client = new WebClient(BrowserVersion.CHROME); client.getOptions().setJavaScriptEnabled(true); client.getOptions().setThrowExceptionOnScriptError(false); client.getOptions().setThrowExceptionOnFailingStatusCode(false); HtmlPage page = client.getPage("http://yoursite.com/"); HtmlTextInput user = page.getElementByName("USER_ID"); user.setValueAttribute("youruser"); HtmlPasswordInput password = page.getElementByName("PWD"); password.setValueAttribute("yourpassword"); HtmlButtonInput loginButton = (HtmlButtonInput) page.getByXPath("//input[@class='loginbtn']").get(0); page = loginButton.click(); System.out.println(page.asText()); client.close();
private void setPageFilter() { try { DomElement purchaseDateFilter = page.getElementByName("purchaseDateFilter"); LOG.info("new URL: " + page.getUrl()); h = page.getElementByName("purchaseDateFilter"); i = h.getSelectedIndex(); if (i != 0) {
@Override protected String getCallbackUrl(final HtmlPage authorizationPage) throws Exception { final HtmlTextInput login = authorizationPage.getElementByName("login"); login.setValueAttribute("testscribeup@gmail.com"); final HtmlPasswordInput password = authorizationPage.getElementByName("passwd"); password.setValueAttribute("testpwdscribeup"); final HtmlSubmitInput submit = authorizationPage.getElementByName("SI"); final HtmlPage callbackPage = submit.click(); final String callbackUrl = callbackPage.getUrl().toString(); logger.debug("callbackUrl : {}", callbackUrl); return callbackUrl; }
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();
/** * Tests {@link ManualTriggerAction.Approval#getApprovals(net.sf.json.JSONObject, int)}. * With an last patchset * * @throws Exception if so. */ @Test public void testDoGerritSearchLastPatchSet() throws Exception { JenkinsRule.WebClient client = j.createWebClient(); HtmlPage page = client.goTo("gerrit_manual_trigger"); HtmlForm theSearch = page.getFormByName("theSearch"); page = j.submit(theSearch); HtmlTable table = page.getElementByName("searchResultTable"); HtmlTableCell verifiedCell = table.getCellAt(FIRST_RESULT_ROW, VERIFIED_COLUMN); DomNode child = verifiedCell.getFirstChild(); assertThat(child, instanceOf(HtmlImage.class)); assertEquals("-1", ((HtmlImage)child).getAltAttribute()); HtmlTableCell codeReviewCell = table.getCellAt(FIRST_RESULT_ROW, CODE_REVIEW_COLUMN); child = codeReviewCell.getFirstChild(); assertThat(child, instanceOf(HtmlImage.class)); assertEquals("2", ((HtmlImage)child).getAltAttribute()); }
/** * Tests {@link ManualTriggerAction.Approval#getApprovals(net.sf.json.JSONObject, int)}. * With an all patchsets * * @throws Exception if so. */ @Test public void testDoGerritSearchAllPatchSets() throws Exception { JenkinsRule.WebClient client = j.createWebClient(); HtmlPage page = client.goTo("gerrit_manual_trigger"); HtmlForm theSearch = page.getFormByName("theSearch"); page = theSearch.getInputByName("allPatchSets").click(); theSearch = page.getFormByName("theSearch"); page = j.submit(theSearch); HtmlTable table = page.getElementByName("searchResultTable"); HtmlTableCell verifiedCell = table.getCellAt(SECOND_RESULT_ROW, VERIFIED_COLUMN); DomNode child = verifiedCell.getFirstChild(); assertThat(child, instanceOf(HtmlImage.class)); assertEquals("-1", ((HtmlImage)child).getAltAttribute()); HtmlTableCell codeReviewCell = table.getCellAt(SECOND_RESULT_ROW, CODE_REVIEW_COLUMN); child = codeReviewCell.getFirstChild(); assertThat(child, instanceOf(HtmlImage.class)); assertEquals("2", ((HtmlImage)child).getAltAttribute()); } }