What is good way to getElementsByClass() with HTTPUnit?
I tried
HTMLElement[] focused = response.getElementsWithAttribute("class", "focused");
I was hoping it would find
<span class="focused">Submit</span>
among others, but all I get back is an empty array.
Google has been no help to me.
So. If you were me and you wanted to get an array of HTMLElements by class name, what would you use?
Edit:
The source
public class ExampleIT extends TestCase {
private final String BASE = "http://localhost:8080/preview.htm?";
@Test
public void testFocusedArePresent() throws MalformedURLException, SAXException, IOException {
WebConversation conversation = new WebConversation();
WebResponse response = conversati开发者_Python百科on.getResponse(BASE + "template=Sample");
HTMLElement[] focused = response.getElementsWithAttribute("class", "focused");
assertTrue(focused.length > 0);
}
}
I hope that helps.
getElementsWithAttribute
works for me. Alternatively, you can try iterating over the DOM, looking for an element with a specified class attribute. Here is some sample code:
Document doc = response.getDOM();
List<Node> result = new ArrayList<Node>();
NodeList list = doc.getElementsByTagName("*");
for(int i = 0 ; i < list.getLength() ; i++){
Node n = list.item(i).getAttributes().getNamedItem("class");
if(n!=null && "focused".equals(n.getNodeValue())){
result.add(n);
}
}
精彩评论