From: Simon S. <sm...@la...> - 2002-08-29 14:10:53
|
On Thu, Aug 29, 2002 at 02:03:20PM +0000, Asle Pedersen wrote: > How can I retrieve htmlAnchors from a HtmlPage if there is no ID on the > anchors to adress them (similar with forms)? E.g. is there a way of getting > a list of all the links/anchors from the page? There's a feature request for this, I think, but I use a chunk of code similar to this: /** Similar to HtmlPage.getAnchorById() but uses the label of the anchor to obtain the element * @param page The page containing the anchor being searched for * @param label The label (user visible part) of the anchor * @return The HtmlAnchor represented by the given label, or null if no label is found. */ private HtmlAnchor getHtmlAnchor(HtmlPage page, String label) { // Make sure that the inputs are sane if (label == null) return null; List anchors = new ArrayList(); anchors.add("a"); List returnedAnchors = page.getHtmlElementsByTagNames( anchors ); Iterator i = returnedAnchors.iterator(); HtmlAnchor anchor = null; while(i.hasNext()) { anchor = (HtmlAnchor)i.next(); if (label.equals(anchor.asText())) return anchor; } return null; } Cheers, Simon -- Asynchronous inputs are at the root of our race problems. -- D. Winker and F. Prosser |