Skip to content

perform_select_option does not persist selection on single <select> elements #277

@barankutluay

Description

@barankutluay

Bug: perform_select_option loses selection on single <select>

Package: Botasaurus Driver
File: botasaurus_driver/core/element.py

perform_select_option() sets option.selected = true on <option> but does not update the parent <select>'s value, causing selections on single <select> elements to be lost when focus changes. Multi-select <select multiple> is unaffected.

Proposed fix:

def perform_select_option(self):
        """
        Marks an <option> as selected and properly updates the parent <select> element
        to persist the selection.
    
        Fixes a bug where single <select> elements lose their selected option
        when focus moves to another input. Multi-select <select multiple> behavior
        remains intact.
    
        Usage: call this method on an Option element returned by select queries.
        """

        if self.node_name == "OPTION":
            self.apply(
                """
                (o) => {
                    const select = o.parentElement;
                    if (select && select.multiple) {
                        o.selected = true;
                    } else {
                        select.value = o.value;
                    }
                    select.dispatchEvent(new Event('input', { bubbles: true }));
                    select.dispatchEvent(new Event('change', { bubbles: true }));
                }
                """
            )

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions