The following html code is from the website I want to automate data using selenium. There is a selectbox and selenium select the required option but the value passed in the server is always zero. Why is that?
html:
<select2 id="selectBranch" name="selectBranch">
<select data-select2-id="1" class="select2-hidden-accessible">
<option value="10" data-select2-id="1">Paris</option>
<option value="20" data-select2-id="2">New York</option>
<option value="30" data-select2-id="3">Tokyo</option>
</select>
</select2>
<button class="btn" type="submit">submit</button>
What I have tried:
# Find the select2 element by its ID and click on it to open the dropdown list
select2_element = driver.find_element("id", "selectBranch")
select2_element.click()
# Find the nested select element by its data-select2-id and create a Select object
select_element = Select(driver.find_element(By.CSS_SELECTOR, "select[data-select2-id='1']"))
# Select the option with value "20" (which is "New York")
select_element.select_by_value("20")
# check if the change event is not occuring in javascript that triggers the selectbox value but it still is not working
driver.execute_script("arguments[0].dispatchEvent(new Event('change'))", select2_element)
# in order to close the select2 element. If the following code is avoided, the options of selectbox remains open
ActionChains(driver).move_to_element(select2_element).click().perform()
# Wait for the name ie.selectBranch of the selectbox to be updated. It still is not working
WebDriverWait(driver, 20).until(EC.text_to_be_present_in_element_value((By.NAME, "selectBranch"), "20"))
Here I've checked if the change event is not occuring in javascript that triggers the selectbox value but it still is not working. Moreover, There's a code to wait for the name ie.selectBranch of the selectbox to be updated. It still is not working.
The main issue here is: When the above code is deployed ie. when the submit btn is clicked it always sends the select2 value "0" instead of "20". P.S since option "New York" is selected, the value "20" should be passed but "0" is passed.
The following request payload is passed all the time, there should be 20 instead of 0.
{
"selectId": 0,
}