0

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,
}
Amrita Stha
  • 2,973
  • 3
  • 25
  • 46

2 Answers2

1

I looked at the SELECT2 and SELECT in the "Depository Participants" dropdown on the login page and was able to get it figured out and working.

from selenium.webdriver.support.ui import Select
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

dropdown = new WebDriverWait(driver, 10).until(EC.visibility_of_element_located(By.CSS_SELECTOR, "#selectBranch > select")))
select = Select(dropdown)
# from here you have several options, pick one... index, value, or text
select.select_by_index(1)
select.select_by_value("129")
select.select_by_visible_text("AGRAWAL SECURITIES PRIVATE LIMITED (12300)")
JeffC
  • 22,180
  • 5
  • 32
  • 55
1

As I rightly suspected and further commented the options are populated through several <li>s within their parent <ul>.

ul_li


Solution

To choose the option with text as AGRAWAL SECURITIES PRIVATE LIMITED (12300) ahe desired element is a Angular element, so to click on the clickable element instead of presence_of_element_located() you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies:

  • Code block:

    driver.get("https://meroshare.cdsc.com.np/#/login")
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "select2#selectBranch span.selection span.select2-selection__placeholder"))).click()
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//ul[@class='select2-results__options']//li[text()='AGRAWAL SECURITIES PRIVATE LIMITED (12300)']"))).click()
    
  • Browser snapshot:

AGRAWAL

  • Note: You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • It worked thankyou. But when I inspect the webpage I cannot see ul, li elements. I searched for select2-results class also but found none. How did you find the ul elements? – Amrita Stha Mar 18 '23 at 09:37