'Populates and submits a form from the keyword arguments.'

def submit_form(self, form_no=0, button='runtool_btn', **kwd):

    for (i, f) in enumerate(self.showforms()):
        if (i == form_no):
            break
    print(("form    '%s'    contains    the    following    controls    (    note    the    values    )" % f.name))
    controls = {}
    formcontrols = self.get_form_controls(f)
    hc_prefix = '<HiddenControl('
    for (i, control) in enumerate(f.controls):
        if (hc_prefix not in str(control)):
            try:
                if (control.name is not None):
                    if ((control.name not in kwd) and control.name.endswith('_add')):
                        repeat_startswith = control.name[0:(-4)]
                        if (repeat_startswith and (not [c_name for c_name in controls.keys() if c_name.startswith(repeat_startswith)]) and [c_name for c_name in kwd.keys() if c_name.startswith(repeat_startswith)]):
                            tc.browser.clicked(f, control)
                            tc.submit(control.name)
                            return self.submit_form(form_no=form_no, button=button, **kwd)
                if (hasattr(control, 'attrs') and ('refresh_on_change' in control.attrs.keys())):
                    changed = False
                    item_labels = [item.attrs['label'] for item in control.get_items() if item.selected]
                    for value in kwd[control.name]:
                        if ((value not in control.value) and (True not in [(value in item_label) for item_label in item_labels])):
                            changed = True
                            break
                    if changed:
                        control.clear()
                        for elem in kwd[control.name]:
                            tc.fv(f.name, control.name, str(elem))
                        control = ClientForm.SubmitControl('SubmitControl', '___refresh_grouping___', {'name': 'refresh_grouping'})
                        control.add_to_form(f)
                        control.fixup()
                        tc.submit('___refresh_grouping___')
                        return self.submit_form(form_no=form_no, button=button, **kwd)
            except Exception:
                log.exception('In    submit_form,    continuing,    but    caught    exception.')
                for formcontrol in formcontrols:
                    log.debug(formcontrol)
                continue
            controls[control.name] = control
    for (control_name, control_value) in kwd.items():
        if (control_name not in controls):
            continue
        if (not isinstance(control_value, list)):
            control_value = [control_value]
        control = controls[control_name]
        control.clear()
        if control.is_of_kind('text'):
            tc.fv(f.name, control.name, ','.join(control_value))
        elif control.is_of_kind('list'):
            try:
                if control.is_of_kind('multilist'):
                    if (control.type == 'checkbox'):
                        def is_checked(value):
                            if (value is True):
                                return True
                            if isinstance(value, list):
                                value = value[0]
                            return (isinstance(value, string_types) and (value.lower() in ('yes', 'true', 'on')))
                        try:
                            checkbox = control.get()
                            checkbox.selected = is_checked(control_value)
                        except Exception as e1:
                            print('Attempting    to    set    checkbox    selected    value    threw    exception:    ', e1)
                            for elem in control_value:
                                control.get(name=elem).selected = True
                    else:
                        for elem in control_value:
                            try:
                                control.get(name=elem).selected = True
                            except Exception:
                                control.get(label=elem).selected = True
                else:
                    for elem in control_value:
                        try:
                            tc.fv(f.name, control.name, str(elem))
                        except Exception:
                            try:
                                if (len(elem) > 30):
                                    elem_name = ('%s..%s' % (elem[:17], elem[(-11):]))
                                    tc.fv(f.name, control.name, str(elem_name))
                                    pass
                                else:
                                    raise
                            except Exception:
                                raise
                        except Exception:
                            for formcontrol in formcontrols:
                                log.debug(formcontrol)
                            log.exception("Attempting    to    set    control    '%s'    to    value    '%s'    (also    tried    '%s')    threw    exception.", control.name, elem, elem_name)
                            pass
            except Exception as exc:
                for formcontrol in formcontrols:
                    log.debug(formcontrol)
                errmsg = ("Attempting    to    set    field    '%s'    to    value    '%s'    in    form    '%s'    threw    exception:    %s\n" % (control_name, str(control_value), f.name, str(exc)))
                errmsg += ('control:    %s\n' % str(control))
                errmsg += 'If    the    above    control    is    a    DataToolparameter    whose    data    type    class    does    not    include    a    sniff()    method,\n'
                errmsg += "make    sure    to    include    a    proper    'ftype'    attribute    to    the    tag    for    the    control    within    the    <test>    tag    set.\n"
                raise AssertionError(errmsg)
        else:
            pass
    tc.submit(button)
