Search code examples
javajythonhtmlunit

selecting pulldown in htmlunit


I am using htmlunit in jython and am having trouble selecting a pull down link. The page I am going to has a table with other ajax links, and I can click on them and move around and it seems okay but I can't seem to figure out how to click on a pulldown menu that allows for more links on the page(this pulldown affects the ajax table so its not redirecting me or anything).

Here's my code:

    selectField1 = page.getElementById("pageNumSelection")
options2 = selectField1.getOptions()
theOption3 = options2[4]

This gets the option I want, I verify its right. so I select it:

MoreOnPage = selectField1.setSelectedAttribute(theOption3, True)

and I am stuck here(not sure if selecting it works or not because I don't get any message, but I'm not sure what to do next. How do I refresh the page to see the larger list? When clicking on links all you have to do is find the link and then select linkNameVariable.click() into a variable and it works. but I'm not sure how to refresh a pulldown. when I try to use the webclient to create an xml page based on the the select variable, I still get the old page.

to make it a bit easier, I used htmlunit scripter and got some code that should work but its java and I'm not sure how to port it to jython. Here it is:

 try
      {
           page = webClient.getPage( url );

           HtmlSelect selectField1 = (HtmlSelect) page.getElementById("pageNumSelection");
           List<HtmlOption> options2 = selectField1.getOptions();
           HtmlOption theOption3 = null;
           for(HtmlOption option: options2)
           {
                if(option.getText().equals("100") )
                {
                     theOption3 = option;
                     break;
                }
           }
           selectField1.setSelectedAttribute(theOption3, true );

Solution

  • Have a look at HtmlForm getSelectedByName

    HtmlSelect htmlSelect = form.getSelectByName("stuff[1].type");

    HtmlOption htmlOption = htmlSelect.getOption(3);

    htmlOption.setSelected(true);