It seems to be a problem to find element with path like //select[@name='cookie']/option[position()=2 and @selected] after the element //select[@name='cookie']/option[position()=2] was selected. Here is the excerpt from the testcase:
public void testSetSelectFieldByIndex() throws Exception {
WebClient client = new WebClient();
HtmlPage page = client.getPage(new URL("http://www.ups.com"));
HtmlOption option = page.getFirstByXPath("//select[@name='cookie']/option[position()=2]");
option.setSelected(true);
HtmlOption selectedOption = page.getFirstByXPath("//select[@name='cookie']/option[position()=2 and @selected]");
assertNotNull("selectedOption is null ", selectedOption);
}
Make sure you don't have cookies from ups.com, although I don't think it makes any difference.
The testcase is attached.
The testcase works with HtmlUnit 2.5, but doesn't work with SNAPSHOT 2.6
Test case for @selected problem
I think that this was an incorrect behaviour of HtmlUnit-2.5 and that current behaviour is the right one. If you try to do the same with Firefox, you'll see that the second XPath expression evaluates to nothing as well. XPath is not very good to find selected option as the "selected status" is not an attribute in the DOM.