List<HtmlAnchor> articles = (List<HtmlAnchor>) page.getByXPath("//a[@class='lblentrylink']");
HtmlPage currentPage = (HtmlPage) webClient.getPage(newURL("http://www.animewallpapers.com")); @SuppressWarnings("unchecked") List<HtmlImage> urls = (List<HtmlImage>) currentPage.getByXPath("//img"); for(int i = 0; i < 500; i++){ currentPage = (HtmlPage) webClient.getPage(new URL(urls.get(i).toString())); @SuppressWarnings("unchecked") List<HtmlImage> tempListUrls = (List<HtmlImage>) currentPage.getByXPath("//img");
/** * Verifies that the specified page contains an input element with the specified name. * * @param page the page to check * @param name the name of the input element to look for */ public static void assertInputPresent(final HtmlPage page, final String name) { final String xpath = "//input[@name='" + name + "']"; final List<?> list = page.getByXPath(xpath); if (list.isEmpty()) { throw new AssertionError("Unable to find an input element named '" + name + "'."); } }
/** * Verifies that the specified page does not contain an input element with the specified name. * * @param page the page to check * @param name the name of the input element to look for */ public static void assertInputNotPresent(final HtmlPage page, final String name) { final String xpath = "//input[@name='" + name + "']"; final List<?> list = page.getByXPath(xpath); if (!list.isEmpty()) { throw new AssertionError("Unable to find an input element named '" + name + "'."); } }
/** * Verifies that the specified page contains an input element with the specified name. * * @param page the page to check * @param name the name of the input element to look for */ public static void assertInputPresent(final HtmlPage page, final String name) { final String xpath = "//input[@name='" + name + "']"; final List< ? > list = page.getByXPath(xpath); if (list.isEmpty()) { throw new AssertionError("Unable to find an input element named '" + name + "'."); } }
/** * Verifies that the specified page does not contain an input element with the specified name. * * @param page the page to check * @param name the name of the input element to look for */ public static void assertInputNotPresent(final HtmlPage page, final String name) { final String xpath = "//input[@name='" + name + "']"; final List< ? > list = page.getByXPath(xpath); if (!list.isEmpty()) { throw new AssertionError("Unable to find an input element named '" + name + "'."); } }
/** * Verifies that the specified page does not contain an input element with the specified name. * * @param page the page to check * @param name the name of the input element to look for */ public static void assertInputNotPresent(final HtmlPage page, final String name) { final String xpath = "//input[@name='" + name + "']"; final List< ? > list = page.getByXPath(xpath); if (!list.isEmpty()) { throw new AssertionError("Unable to find an input element named '" + name + "'."); } }
/** * Verifies that the specified page does not contain an input element with the specified name. * * @param page the page to check * @param name the name of the input element to look for */ public static void assertInputNotPresent(final HtmlPage page, final String name) { final String xpath = "//input[@name='" + name + "']"; final List< ? > list = page.getByXPath(xpath); if (!list.isEmpty()) { throw new AssertionError("Unable to find an input element named '" + name + "'."); } }
final WebClient webClient = new WebClient(); final HtmlPage page = webClient.getPage("http://url.to.your.data"); //get temperatures HtmlTableDataCell minTemp = page.getByXPath("//td[@class='minS']").get(0); HtmlTableDataCell maxTemp = page.getByXPath("//td[@class='maxS']").get(0); HtmlTableHeaderCell date = page.getByXPath("//th[@class='arkaTrh']").get(0); System.out.println("Forecast for " + date.asText() + " - Min: " + minTemp.asText() + ", Max: " + maxTemp.asText());
@Test public void xpath() throws Exception { try (final WebClient webClient = new WebClient()) { final HtmlPage page = webClient.getPage("http://htmlunit.sourceforge.net"); //get list of all divs final List<?> divs = page.getByXPath("//div"); //get div which has a 'name' attribute of 'John' final HtmlDivision div = (HtmlDivision) page.getByXPath("//div[@name='John']").get(0); } }
try (final WebClient webClient = new WebClient(BrowserVersion.CHROME)) { String url = "http://www.gelbeseiten.de/schneider/hamburg"; HtmlPage htmlPage = webClient.getPage(url); for (Object o : htmlPage.getByXPath("//span[@class='teilnehmertelefon']")) { System.out.println(((HtmlElement) o).asXml()); } }
HtmlPage page1 = webClient.getPage("myURI"); HtmlElement htmlElement = (HtmlElement) page1.getByXPath("loginHiddenButtonXPATH").get(0); webClient.setJavaScriptEnabled(false); page1 = htmlElement.click(); webClient.setJavaScriptEnabled(true);
@Override public List<IElement> getElementsByXPath(String xpath) { List<IElement> children = new ArrayList<IElement>(); for (Object child : getCurrentPage().getByXPath(xpath)) { if (child instanceof HtmlElement) { children.add(new HtmlUnitElementImpl((HtmlElement) child)); } } return children; }
public void xpath() throws Exception { final WebClient webClient = new WebClient(); final HtmlPage page = webClient.getPage("http://htmlunit.sourceforge.net"); //get list of all divs final List<?> divs = page.getByXPath("//div"); //get div which has a 'name' attribute of 'John' final HtmlDivision div = (HtmlDivision) page.getByXPath("//div[@name='John']/button").get(0); webClient.closeAllWindows(); }
XPath is the suggested way for more complex searches, a brief tutorial can be found in W3Schools @Test public void xpath() throws Exception { final WebClient webClient = new WebClient(); final HtmlPage page = webClient.getPage("http://htmlunit.sourceforge.net"); //get list of all divs final List<?> divs = page.getByXPath("//div"); //get div which has a 'name' attribute of 'John' final HtmlDivision div = (HtmlDivision) page.getByXPath("//div[@name='John']").get(0); webClient.closeAllWindows(); }
try (WebClient webClient = new WebClient()) { HtmlPage page = webClient.getPage("http://localhost/test.html"); Object o = page.getByXPath("//span[@data-reactid='.3.1.0.0.1:1.0.1.0.1:$0.$=12:$2.2']"); System.out.println(o); }
WebClient client = new WebClient; HtmlPage page = client.getPage("http://www.exapmle.com"); client.waitForBackgroundJavaScript(5 * 1000); Thread.sleep(10*1000);// this code will waite to 10 seconds HtmlUnorderedList ul = (HtmlUnorderedList) page.getByXPath("//ul[contains(@class, 'class-name')]").get(0); HtmlListItem li = (HtmlListItem) ul.getChildNodes().get(1); // I want to click li and get result page. But it takes a little time to execute. li.click(); client.waitForBackgroundJavaScript(5 * 1000); // this code will waite to 10 seconds Thread.sleep(10*1000);
public List<WebElement> findElementsByXPath(String selector) { if (!(lastPage() instanceof HtmlPage)) return new ArrayList<WebElement>(); List<?> nodes = ((HtmlPage) lastPage()).getByXPath(selector); return convertRawHtmlElementsToWebElements(nodes); }
WebClient wc = new WebClient(); wc.setThrowExceptionOnFailingStatusCode(false); wc.setThrowExceptionOnScriptError(false); HtmlPage p = wc.getPage("http://flickr.com"); HtmlImage img = (HtmlImage) p.getByXPath("//img").get(3); MyBase64EncodingReader reader = new MyBase64EncodingReader(img); System.out.println(reader.toString());
@Test public void notAddChildren() throws Exception { JenkinsRule.WebClient client = r.createWebClient(); SampleComputedFolder s = r.jenkins.createProject(SampleComputedFolder.class, "s"); assertEquals(client.getPage(s).getByXPath("//a[contains(text(), \"New Item\")]").size(), 0); s.kids.add("A"); s.recompute(Result.SUCCESS); assertEquals(client.getPage(s).getByXPath("//a[contains(text(), \"New Item\")]").size(), 0); }